@@ -18,66 +18,89 @@ describe("Wrapper lifetime leak probe", function () {
1818 // Each probe dumps the Java heap, so every shape is planted once and probed once, on a
1919 // later task: a WeakRef keeps its target alive until the job that made it ends.
2020 beforeAll ( function ( done ) {
21+ // A cycle through Java whose object's canary was rebuilt since: the record also
22+ // keeps a copy of the rebuilt canary, which must not read as a root.
2123 ( function ( ) {
22- // A JS property referencing its own object.
23- var selfReferencing = new java . lang . Runnable ( { run : function ( ) { } } ) ;
24- selfReferencing . probeSelf = selfReferencing ;
25- planted . push ( new WeakRef ( selfReferencing ) ) ;
26-
27- var twin = new java . lang . Runnable ( { run : function ( ) { } } ) ;
28- twin . probeSelf = twin ;
29- planted . push ( new WeakRef ( twin ) ) ;
30-
31- // A cycle through Java: the object's state holds a list that holds the object.
32- var listed = new java . lang . Runnable ( { run : function ( ) { } } ) ;
24+ var rebuilt = new java . lang . Runnable ( { run : function ( ) { } } ) ;
3325 var list = new java . util . ArrayList ( ) ;
34- list . add ( listed ) ;
35- listed . probeList = list ;
36- planted . push ( new WeakRef ( listed ) ) ;
37-
38- // Held by a Java static: alive for a reason of its own, not a leak.
39- var held = new java . lang . Runnable ( { run : function ( ) { } } ) ;
40- held . probeHeld = held ;
41- Holder . held = held ;
42- planted . push ( new WeakRef ( held ) ) ;
43-
44- // A WeakMap value referencing its own key: in canary mode the key is the
45- // object's lifetime key, which its state holds.
46- var mapped = new java . lang . Runnable ( { run : function ( ) { } } ) ;
47- probeMap . set ( mapped , { probeMapped : mapped } ) ;
48- planted . push ( new WeakRef ( mapped ) ) ;
49-
50- // A back reference through a WeakRef keeps nothing.
51- var weak = new java . lang . Runnable ( { run : function ( ) { } } ) ;
52- weak . probeWeak = new WeakRef ( weak ) ;
53- } ) ( ) ;
54- ( function ( ) {
55- // A method closing over its own object: the cycle runs through the object's
56- // prototype, its implementation object. In a function of its own, so no other
57- // planted closure shares the context that holds it.
58- var probeClosed = new java . lang . Runnable ( {
59- run : function ( ) {
60- return probeClosed ;
61- }
62- } ) ;
63- planted . push ( new WeakRef ( probeClosed ) ) ;
26+ list . add ( rebuilt ) ;
27+ rebuilt . probeRebuiltList = list ;
28+ planted . push ( new WeakRef ( rebuilt ) ) ;
29+ Holder . held = list ;
6430 } ) ( ) ;
31+ // The canary dies on a later job: a WeakRef keeps its target until the job ends.
6532 setTimeout ( function ( ) {
66- report = runtime . findLeakCycles ( ) ;
67- console . log ( "leak probe: " + JSON . stringify ( {
68- leaked : report . leaked ,
69- leakedOutsideCycles : report . leakedOutsideCycles ,
70- probed : report . probed ,
71- cycles : report . cycles . length ,
72- durationMs : report . durationMs ,
73- dumpBytes : report . dumpBytes
74- } ) ) ;
75- report . cycles . forEach ( function ( cycle ) {
76- console . log ( "leak probe cycle: " + cycle . signature ) ;
77- } ) ;
78- done ( ) ;
33+ gc ( ) ;
34+ java . lang . System . nanoTime ( ) ;
35+ java . lang . Runtime . getRuntime ( ) . gc ( ) ;
36+ gc ( ) ;
37+ java . lang . System . nanoTime ( ) ;
38+ ( function ( ) {
39+ // A read: the rebuilt canary's copy stays current.
40+ Holder . held . get ( 0 ) . probeRebuiltList . size ( ) ;
41+ } ) ( ) ;
42+ Holder . held = null ;
43+ ( function ( ) {
44+ // A JS property referencing its own object.
45+ var selfReferencing = new java . lang . Runnable ( { run : function ( ) { } } ) ;
46+ selfReferencing . probeSelf = selfReferencing ;
47+ planted . push ( new WeakRef ( selfReferencing ) ) ;
48+
49+ var twin = new java . lang . Runnable ( { run : function ( ) { } } ) ;
50+ twin . probeSelf = twin ;
51+ planted . push ( new WeakRef ( twin ) ) ;
52+
53+ // A cycle through Java: the object's state holds a list that holds the object.
54+ var listed = new java . lang . Runnable ( { run : function ( ) { } } ) ;
55+ var list = new java . util . ArrayList ( ) ;
56+ list . add ( listed ) ;
57+ listed . probeList = list ;
58+ planted . push ( new WeakRef ( listed ) ) ;
59+
60+ // Held by a Java static: alive for a reason of its own, not a leak.
61+ var held = new java . lang . Runnable ( { run : function ( ) { } } ) ;
62+ held . probeHeld = held ;
63+ Holder . held = held ;
64+ planted . push ( new WeakRef ( held ) ) ;
65+
66+ // A WeakMap value referencing its own key: in canary mode the key is the
67+ // object's lifetime key, which its state holds.
68+ var mapped = new java . lang . Runnable ( { run : function ( ) { } } ) ;
69+ probeMap . set ( mapped , { probeMapped : mapped } ) ;
70+ planted . push ( new WeakRef ( mapped ) ) ;
71+
72+ // A back reference through a WeakRef keeps nothing.
73+ var weak = new java . lang . Runnable ( { run : function ( ) { } } ) ;
74+ weak . probeWeak = new WeakRef ( weak ) ;
75+ } ) ( ) ;
76+ ( function ( ) {
77+ // A method closing over its own object: the cycle runs through the object's
78+ // prototype, its implementation object. In a function of its own, so no other
79+ // planted closure shares the context that holds it.
80+ var probeClosed = new java . lang . Runnable ( {
81+ run : function ( ) {
82+ return probeClosed ;
83+ }
84+ } ) ;
85+ planted . push ( new WeakRef ( probeClosed ) ) ;
86+ } ) ( ) ;
87+ setTimeout ( function ( ) {
88+ report = runtime . findLeakCycles ( ) ;
89+ console . log ( "leak probe: " + JSON . stringify ( {
90+ leaked : report . leaked ,
91+ leakedOutsideCycles : report . leakedOutsideCycles ,
92+ probed : report . probed ,
93+ cycles : report . cycles . length ,
94+ durationMs : report . durationMs ,
95+ dumpBytes : report . dumpBytes
96+ } ) ) ;
97+ report . cycles . forEach ( function ( cycle ) {
98+ console . log ( "leak probe cycle: " + cycle . signature ) ;
99+ } ) ;
100+ done ( ) ;
101+ } , 0 ) ;
79102 } , 0 ) ;
80- } ) ;
103+ } , 20000 ) ;
81104
82105 afterAll ( function ( ) {
83106 Holder . held = null ;
@@ -159,6 +182,15 @@ describe("Wrapper lifetime leak probe", function () {
159182 expect ( cycles [ 0 ] . path ) . toContain ( "--ns:wrapper-->" ) ;
160183 } ) ;
161184
185+ it ( "finds a cycle through Java after the object's canary was rebuilt" , function ( ) {
186+ var cycles = cyclesThrough ( "probeRebuiltList" ) ;
187+ if ( ! canary ) {
188+ expect ( cycles . length ) . toBe ( 0 ) ;
189+ return ;
190+ }
191+ expect ( cycles . length ) . toBe ( 1 ) ;
192+ } ) ;
193+
162194 it ( "does not report an object Java holds for a reason of its own" , function ( ) {
163195 expect ( cyclesThrough ( "probeHeld" ) . length ) . toBe ( 0 ) ;
164196 } ) ;
0 commit comments