Skip to content

Instantly share code, notes, and snippets.

@kimble
Forked from ChristinGorman/gist:67be1561c20b2fdaf0d5
Last active August 29, 2015 14:18
Show Gist options
  • Save kimble/874304cac45ea31f4018 to your computer and use it in GitHub Desktop.
Save kimble/874304cac45ea31f4018 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import java.lang.ref.WeakReference;
import static org.junit.Assert.*;
public class PubSubTest {
@Test
public void notWtf() {
WeakReference<Runnable> reference = new WeakReference<Runnable>(new Runnable() {
@Override
public void run() {
// ...
}
});
forceGc();
assertNull(reference.get());
}
@Test
public void lambdaWtf() {
WeakReference<Runnable> reference = new WeakReference<Runnable>(() -> Integer.valueOf(1) );
forceGc();
assertNull(reference.get());
}
public void forceGc() {
try {
Object[] ignored = new Object[(int) Runtime.getRuntime().maxMemory()];
} catch (Throwable e) {
// Ignore OME
}
System.gc();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment