daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 308 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a garbage collector valami heurisztika szerint fut le a js-ben, de te tudod forcolni
  2. // ha igy inditod a kodot: node --expose-gc file.js
  3.  
  4. /*
  5.  Minden kiiratas elott lefuttatjuk a GC-t, hogy csak a tiszta Heap-et lassuk MB-ban
  6.  */
  7. function getHeapMb() {
  8.   global.gc();
  9.   return Math.round(process.memoryUsage().heapUsed * 10 / 1024 /1024)/10 + 'MB';
  10. }
  11.  
  12. /*
  13.   ==================================== 1. ==============================
  14.   Az elso peldaban 1 object van, parameterben adom at a rec fuggvenynek
  15.   Itt az obj az mindig ugyanarra az objectre fog mutatni, a heapen nem jon letre uj
  16.  */
  17. console.log('#1 - recursive start: ', getHeapMb());
  18.  
  19. function rec(i, obj) {
  20.   obj.counter = i;
  21.   if(i===0) {
  22.     console.log('#2 - recursive break: ', getHeapMb());
  23.     return;
  24.   }
  25.   rec(--i, obj);
  26. }
  27.  
  28. const obj = {};
  29. rec(8000, obj);
  30.  
  31. console.log('#1 - recursive end: ', getHeapMb());
  32. console.log('');
  33.  
  34.  
  35. /*
  36.   ==================================== 2. ==============================
  37.   Az masodik peldaban az objectet mindig a recurziv fuggvenyben hozom letre
  38.   Minden letrehozaskor uj memoria foglalodik le a heapen, amit a garbage NEM fog felszabaditani
  39.  */
  40. console.log('#2 - recursive start: ', getHeapMb());
  41.  
  42. function rec2(i, obj) {
  43.   obj = { counter: i };
  44.   if(i===0) {
  45.     console.log('#2 - recursive break: ', getHeapMb());
  46.     return;
  47.   }
  48.   rec2(--i, obj);
  49. }
  50.  
  51. const obj2 = {};
  52. rec2(8000, obj2);
  53.  
  54. console.log('#2 - recursive end: ', getHeapMb());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top