Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

75
LINES

< > BotCompany Repo | #1035344 // TestPhantomCleanUp - also tests RunnablesReferenceQueue

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9719L/55K).

1  
sclass TestPhantomCleanUp {
2  
  settable bool withGC;
3  
  settable double maxWait = 60.0; // how many seconds
4  
  
5  
  gettable bool referenceWasCleared;
6  
  gettable Duration delayBeforeClearance;
7  
  
8  
  // The object to be referenced & cleared by the GC.
9  
  // Don't keep any references to it, of course!
10  
  swappable O createReferent() {
11  
    ret new S("copy of a string");
12  
  }
13  
14  
  run {
15  
    temp new RunnablesReferenceQueue queue;
16  
    
17  
    class MyPhantomRef<A> extends PhantomReference<A> is Runnable {
18  
      new Flag flag;
19  
    
20  
      *(A a, RunnablesReferenceQueue queue) {
21  
        super(a, queue!);
22  
      }
23  
    
24  
      run {
25  
        printWithMS("Behold the phantom!");
26  
        flag.raise();
27  
      }
28  
    }
29  
    
30  
    var ref = new MyPhantomRef<O>(createReferent(), queue);
31  
    printWithMS("Made the object.");
32  
    
33  
    if (withGC) {
34  
      print("GC.");
35  
      timedGC();
36  
    }
37  
    long time = sysNow();
38  
    printWithMS("Now waiting...");
39  
    if (ref.flag.waitUntilUp(toMS_int(maxWait))) {
40  
      delayBeforeClearance = msToDuration(sysNow()-time);
41  
      referenceWasCleared = true;
42  
      printWithMS("Cool! Reference was cleared after "
43  
        + renderElapsedSecondsPleasantly(delayBeforeClearance)
44  
        + " (" + withOrWithout(withGC) + " GC)");
45  
    } else
46  
      printWithMS("Ouch. Reference not cleared even after " + maxWait + " s");
47  
  }
48  
  
49  
  svoid allTests {
50  
    testSmallObject();
51  
    testHumungousObject();
52  
  }
53  
  
54  
  svoid testSmallObject {
55  
    for (gc : falseTrue()) {
56  
      printAfterNL("Small object test!");
57  
      new TestPhantomCleanUp().withGC(gc).run();
58  
    }
59  
  }
60  
  
61  
  svoid testHumungousObject {
62  
    for (gc : falseTrue()) {
63  
      printAfterNL("Humungous object test!");
64  
      
65  
      var tpcu = new TestPhantomCleanUp().withGC(gc);
66  
      
67  
      // pretty likely to be humungous
68  
      // Def. humungous: an array bigger than (G1 region size/2)
69  
      // Check "jinfo -flags $pid" to see the region size
70  
      tpcu.createReferent = -> new byte[8*oneMegabyte_int()];
71  
      
72  
      tpcu.run();
73  
    }
74  
  }
75  
}

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: 86 / 181
Version history: 15 change(s)
Referenced in: [show references]