memory_in_javascript.pdf

 memory_in_javascript.pdf

Transcript

  1. 5.

    If space is not exists HEAP { a: 1, b:

    2 } I wanna create an object!! I can’t allocate no more!
  2. 6.

    Search spaces HEAP { a: 1, b: 2 } I

    wanna create an object!! I found unused space
  3. 8.

    Marking Phase HEAP 1 2 3 4 5 This object

    is not referenced from anywhere
  4. 10.
  5. 12.

    Generational GC Allocated objects are managed by separete generation, to

    more efficient sweeping. If VM can’t keep enough memory space, VM starting sweeping Young Generation, so Young Generation may be held many Short-lived objects and possibility that is found empty space is higher. This called Minor GC. If empty spaces are not found in Young Generation, VM start sweeping Old Generation. This called Major GC.
  6. 13.

    Memory leak In many case, Javascript will not leak memory

    today. MarkSweep GC has solve cyclic reference. But MarkSweep is not a silver bullet, if you holding many large object, that will still cause memory exhaustion.
  7. 15.

    Cleanup object const indices = {}; indices['testA'] = new SomeClass();

    indices['testB'] = new SomeClass(); delete indices['testA']; delete indices['testB'];
  8. 16.

    Cleanup object but … const indices = {}; indices['testA'] =

    new SomeClass(); indices['testB'] = new SomeClass(); delete indices['testA']; delete indices['testB']; When can we delete these objects?
  9. 17.

    Finalizer We want to delete these, when SomeClass will be

    swept. HEAP 4PNF $MBTT 1 2 3 4 This time
  10. 18.

    WeakMap const indices = new WeakMap(); const a = new

    SomeClass(); const b = new SomeClass(); indices.set(a, someValue); indices.set(b, someValue);
  11. 19.

    WeakMap hooks When an object whose registered as the key

    of a WeakMap deleted, the bound value will be deleted too. HEAP 4PNF $MBTT 1 2 3 4 WeakMap WeakMap delete a bound value of SomeClass.
  12. 20.

    But WeakMap not match some situations… const cache = {};

    function cache(factory) { return key => { const ret = cache[key]; if (ret) { return ret; } return cache[key] = factory(); } }
  13. 21.
  14. 22.

    What is WeakRef? WeakRef is now proposed ecamscript functionality that

    enable user to be concerned to Object lifetime and GarbageCollection. WeakRef is now stage-2
  15. 23.

    WeakRef function makeWeakCached(f) { const cache = new Map(); return

    key => { const ref = cache.get(key); if (ref) { const cached = ref.deref(); if (cached !== undefined) return cached; } const fresh = f(key); cache.set(key, new WeakRef(fresh)); return fresh; }; }
  16. 24.

    How WeakRef works? WeakRef will holds a value without references.

    So, Garbage Collector will not traverse object whose held by WeakRef.
  17. 25.

    Hooks for Garbage Collector WeakRef provides finalizer functionality for GC.

    If finalizer callback is registered, that callback will be called when object is swept.
  18. 26.

    WeakRef finalizer const object = {}; const finalizer = new

    FinalizationGroup((holdings) => {}); finalizer.register( object, holdings, token); finalizer.unregister(token);
  19. 27.

    WeakRef finalizer function makeWeakCached(f) { const cache = new Map();

    const cleanup = new FinalizationGroup(iterator => { for (const key of iterator) { // See note below on concurrency considerations. const ref = cache.get(key); if (ref && !ref.deref()) cache.delete(key); } }); return key => { const ref = cache.get(key); if (ref) { const cached = ref.deref(); // See note below on concurrency considerations. if (cached !== undefined) return cached; } const fresh = f(key); cache.set(key, new WeakRef(fresh)); cleanup.register(fresh, key, key); return fresh; }; }
  20. 28.

    WeakRef HEAP 1 2 3 4 5 The object referenced

    by WeakRef is not treat as referenced object
  21. 29.

    WeakRef hooks When an object whose registered to FinalizerGroup is

    deleted, Finalizer callback will be invoked. HEAP  1 2 3 4 FinalizerGroup FinalizerGroup callback will be called
  22. 30.

    Warnings Finalizer might not be called, because implementations of GC

    are different from each engines and sweep timing is different from each object or may not be deleted until end of process. So above reasons, you should not use Finalizer as a resource cleanup process.