• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/60_gc

  1 The `gc()` function allows controlling the garbage collector of the VM.
  2 It takes the requested operation as first argument and an optional,
  3 operation specific second argument.
  4 
  5 Returns `null` if the given operation is invalid or if the operation
  6 specific argument is invalid.
  7 
  8 Returns `false` if the requested operation would not result in any
  9 changes.
 10 
 11 Returns `true` if the requested oepration succeeded (e.g. starting
 12 the GC when it was previously stopped).
 13 
 14 Returns an object count if the given operation is `count`.
 15 
 16 -- Testcase --
 17 {%
 18         printf("Count #1: %d\n", gc("count"));
 19 
 20         // create an unreachable cyclic structure
 21         let o = {};
 22         o.cycle = o;
 23         o = null;
 24 
 25         printf("Count #2: %d\n", gc("count"));
 26 
 27         // invoking gc without any argument defaults to "collect"
 28         gc();
 29 
 30         printf("Count #3: %d\n", gc("count"));
 31 
 32 
 33         // create another unreachable cyclic structure
 34         o = {};
 35         o.cycle = o;
 36         o = null;
 37 
 38         printf("Count #4: %d\n", gc("count"));
 39 
 40         // invoking gc with explicit collect argument
 41         gc("collect");
 42 
 43         printf("Count #5: %d\n", gc("count"));
 44 %}
 45 -- End --
 46 
 47 -- Expect stdout --
 48 Count #1: 6
 49 Count #2: 7
 50 Count #3: 6
 51 Count #4: 7
 52 Count #5: 6
 53 -- End --
 54 
 55 
 56 Testing enabling the automatic collector.
 57 
 58 -- Testcase --
 59 {%
 60         // start GC, trigger every 10 object allocations
 61         gc("start", 10);
 62 
 63         for (let i = 0; i < 100; i++) {
 64                 let o = {};
 65                 o.cyle = o;
 66                 o = null;
 67 
 68                 if ((i % 10) == 0)
 69                         printf("Count #%d: %d\n", (i / 10) + 1, gc("count"));
 70         }
 71 
 72         // stop GC
 73         gc("stop");
 74 
 75         for (let i = 100; i < 200; i++) {
 76                 let o = {};
 77                 o.cyle = o;
 78                 o = null;
 79 
 80                 if ((i % 10) == 0)
 81                         printf("Count #%d: %d\n", (i / 10) + 1, gc("count"));
 82         }
 83 %}
 84 -- End --
 85 
 86 -- Expect stdout --
 87 Count #1: 7
 88 Count #2: 14
 89 Count #3: 14
 90 Count #4: 14
 91 Count #5: 14
 92 Count #6: 14
 93 Count #7: 14
 94 Count #8: 14
 95 Count #9: 14
 96 Count #10: 14
 97 Count #11: 14
 98 Count #12: 24
 99 Count #13: 34
100 Count #14: 44
101 Count #15: 54
102 Count #16: 64
103 Count #17: 74
104 Count #18: 84
105 Count #19: 94
106 Count #20: 104
107 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt