Skip to content

Memory leak in global.postMessage #1358

Closed
@patricklx

Description

@patricklx

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 5.3.1
  • Cross-platform modules: 5.3.1
  • Android Runtime: 5.3.1
  • iOS Runtime (if applicable):
  • Plugin(s):

Describe the bug
Memory leak in worker thread. We also observe memory increase.
in https://github.com/NativeScript/android-runtime/blob/master/test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp#L22
It looks like seen is reused by the replacer.

To Reproduce
Im calling global.postMessage many times.

to observe the bug, globaly override:

const push = Array.prototype.push;
Array.prototype.push = function(...args) {
    push.call(this, ...args);
    if (this.length > 500) {
      console.trace('length > 500', this.length);
    }
  };

I observed the trace to go over 40.000
Memory also increases over time.

Expected behavior

Array/memory should not increase so much.
Current fix:

override stringify

const stringify = JSON.stringify;
const stringifyCircular = function(o) {
  const cache = [];
  return stringify(o, function(key, value) {
    if (typeof value === 'object' && value !== null) {
      if (cache.indexOf(value) !== -1) {
        // Duplicate reference found
        try {
          // If this value does not reference a parent it can be deduped
          return JSON.parse(stringify(value));
        } catch (error) {
          // discard key if value cannot be deduped
          return '[circular]';
        }
      }
      // Store value in our collection
      cache.push(value);
    }
    return value;
  });
};
JSON.stringify = stringifyCircular;

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions