Skip to content

Memory leak in global.postMessage #1358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
patricklx opened this issue Apr 29, 2019 · 2 comments
Closed

Memory leak in global.postMessage #1358

patricklx opened this issue Apr 29, 2019 · 2 comments
Assignees
Milestone

Comments

@patricklx
Copy link

patricklx commented Apr 29, 2019

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;
@patricklx
Copy link
Author

patricklx commented Apr 30, 2019

update:
looks like this was caused because we do Obect.freeze(global)... (We had our reasons for it, but I guess we will remove it)

Also, the seen variable is global and not local, so I suggest to add var,let const to it.

@vtrifonov
Copy link
Contributor

Thanks for pointing it, we've added const fo the seen variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants