memory-leak.js 880 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env node --expose_gc
  2. var weak = require('weak');
  3. var test = require('tap').test
  4. var LRU = require('../')
  5. var l = new LRU({ max: 10 })
  6. var refs = 0
  7. function X() {
  8. refs ++
  9. weak(this, deref)
  10. }
  11. function deref() {
  12. refs --
  13. }
  14. test('no leaks', function (t) {
  15. // fill up the cache
  16. for (var i = 0; i < 100; i++) {
  17. l.set(i, new X);
  18. // throw some gets in there, too.
  19. if (i % 2 === 0)
  20. l.get(i / 2)
  21. }
  22. gc()
  23. var start = process.memoryUsage()
  24. // capture the memory
  25. var startRefs = refs
  26. // do it again, but more
  27. for (var i = 0; i < 10000; i++) {
  28. l.set(i, new X);
  29. // throw some gets in there, too.
  30. if (i % 2 === 0)
  31. l.get(i / 2)
  32. }
  33. gc()
  34. var end = process.memoryUsage()
  35. t.equal(refs, startRefs, 'no leaky refs')
  36. console.error('start: %j\n' +
  37. 'end: %j', start, end);
  38. t.pass();
  39. t.end();
  40. })