Skip to content

Instantly share code, notes, and snippets.

@meoyawn
Last active November 8, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoyawn/70d821957ac6b3b54dc7 to your computer and use it in GitHub Desktop.
Save meoyawn/70d821957ac6b3b54dc7 to your computer and use it in GitHub Desktop.
play a very scary sound on each GC trigger
import rx.Observable
import rx.Subscriber
import rx.schedulers.Schedulers
import java.lang.ref.PhantomReference
import java.lang.ref.ReferenceQueue
fun gcTriggers(): Observable<Unit> =
Observable.create(triggerLoop())
.subscribeOn(Schedulers.newThread())
private fun triggerLoop() = { sub: Subscriber<in Unit> ->
Thread.currentThread().priority = Thread.MIN_PRIORITY
val queue = ReferenceQueue<Any>()
var phantom = PhantomReference(Any(), queue)
while (!sub.isUnsubscribed) {
// blocks for up to 16 ms until the object is GC'd
if (queue.remove(16) != null) {
sub.onNext(Unit)
phantom = PhantomReference(Any(), queue)
}
}
}
import android.media.RingtoneManager
val ringtone = lazy {
val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
RingtoneManager.getRingtone(this, uri)
}
gcTriggers().subscribe {
val ring = ringtone.value
ring.stop()
ring.play()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment