diagnostics icon indicating copy to clipboard operation
diagnostics copied to clipboard

Detecting memory leaks through shared function context

Open hashseed opened this issue 6 years ago • 5 comments

This was discussed at the diagnostics summit, and I want to take the discussion here. The document I referred to is this, by my colleague @jakobkummerow.

The gist of the problem is:

const f = (function() {
  let a = {};
  let b = {};
  let c = ()=>a;  // forces a to be context-allocated.
  let d = ()=>b;  // forces b to be context-allocated.
  return d;  // the function context escapes and is kept alive.
})();

The object assigned to a is no longer accessible though JavaScript, but is kept alive because it is allocated onto the same context as b, which remains accessible through d.

Detecting this pattern at runtime would require the VM to keep track of additional meta-data, which does not pay off. However, we could, based on the information from a heap snapshot reconstruct missing metadata and detect this pattern.

hashseed avatar Mar 18 '19 10:03 hashseed

@BridgeAR @mcollina

hashseed avatar Mar 18 '19 10:03 hashseed

The document is not publicly accessible.

Having some automated tool that can detect this pattern would be fantastic. Note that this happens quite frequently in Node.js code and the vast majority of it is not a memory leak. Tracking and resolving those would be equally valuable, mainly because they will increase the memory footprint and simplify the GC job.

mcollina avatar Mar 18 '19 10:03 mcollina

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

github-actions[bot] avatar Jul 17 '20 00:07 github-actions[bot]

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

github-actions[bot] avatar Jul 23 '22 00:07 github-actions[bot]

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

github-actions[bot] avatar Oct 22 '22 00:10 github-actions[bot]