| 1 | // aggressively collects garbage until we fail to improve terminatingIterations |
| 2 | // times. |
| 3 | function garbageCollect() { |
| 4 | var terminatingIterations = 3; |
| 5 | var usedBeforeGC = Number.MAX_VALUE; |
| 6 | var nondecreasingIterations = 0; |
| 7 | for ( ; ; ) { |
| 8 | global.gc(); |
| 9 | var usedAfterGC = process.memoryUsage().heapUsed; |
| 10 | if (usedAfterGC >= usedBeforeGC) { |
| 11 | nondecreasingIterations++; |
| 12 | if (nondecreasingIterations >= terminatingIterations) { |
| 13 | break; |
| 14 | } |
| 15 | } |
| 16 | usedBeforeGC = usedAfterGC; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | module.exports = garbageCollect; |