Last active
November 8, 2015 20:09
-
-
Save meoyawn/70d821957ac6b3b54dc7 to your computer and use it in GitHub Desktop.
play a very scary sound on each GC trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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