Libraryless. Click here for Pure Java version (9719L/55K).
sclass TestPhantomCleanUp { settable bool withGC; settable double maxWait = 60.0; // how many seconds gettable bool referenceWasCleared; gettable Duration delayBeforeClearance; // The object to be referenced & cleared by the GC. // Don't keep any references to it, of course! swappable O createReferent() { ret new S("copy of a string"); } run { temp new RunnablesReferenceQueue queue; class MyPhantomRef<A> extends PhantomReference<A> is Runnable { new Flag flag; *(A a, RunnablesReferenceQueue queue) { super(a, queue!); } run { printWithMS("Behold the phantom!"); flag.raise(); } } var ref = new MyPhantomRef<O>(createReferent(), queue); printWithMS("Made the object."); if (withGC) { print("GC."); timedGC(); } long time = sysNow(); printWithMS("Now waiting..."); if (ref.flag.waitUntilUp(toMS_int(maxWait))) { delayBeforeClearance = msToDuration(sysNow()-time); referenceWasCleared = true; printWithMS("Cool! Reference was cleared after " + renderElapsedSecondsPleasantly(delayBeforeClearance) + " (" + withOrWithout(withGC) + " GC)"); } else printWithMS("Ouch. Reference not cleared even after " + maxWait + " s"); } svoid allTests { testSmallObject(); testHumungousObject(); } svoid testSmallObject { for (gc : falseTrue()) { printAfterNL("Small object test!"); new TestPhantomCleanUp().withGC(gc).run(); } } svoid testHumungousObject { for (gc : falseTrue()) { printAfterNL("Humungous object test!"); var tpcu = new TestPhantomCleanUp().withGC(gc); // pretty likely to be humungous // Def. humungous: an array bigger than (G1 region size/2) // Check "jinfo -flags $pid" to see the region size tpcu.createReferent = -> new byte[8*oneMegabyte_int()]; tpcu.run(); } } }
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035344 |
Snippet name: | TestPhantomCleanUp - also tests RunnablesReferenceQueue |
Eternal ID of this version: | #1035344/16 |
Text MD5: | f128ba0df6dc9d8210841118a97b12c0 |
Transpilation MD5: | 637a675d953047de90584823c3e71d8e |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-05-01 19:29:52 |
Source code size: | 2074 bytes / 75 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 146 / 260 |
Version history: | 15 change(s) |
Referenced in: | [show references] |