1

I'm writing a simple chrome extension and I've encountered very weird issue. I have a code that I inject like this

element.src = chrome.runtime.getURL('inject.js');
document.head.appendChild(element);

Injects.js itself:

(function() {
   //my code here
}());

The code itself is nothing special, I use code similar to monkeyPatch to patch some original page functions and receive event data from them and my code on its own only has one anonymous function setTimeout{}, the rest is just data/DOM manipulation.

Now, the weird thing is if I change the function I wrap my code into to IIFE example:

(function () {
    //my code
})();

I start to get insane memoryleak, like, few Gb in 5-7 minutes.

I'm not very verse in JS and its ecosystem - what could cause that? I'd like to understand it and avoid similar issues it in future. Let me know if more context needed.


EDIT: 1. someone linked: Location of parenthesis for auto-executing anonymous JavaScript functions? Interesting how it mentions: Does the former take up memory by leaving around a global, anonymous function?

2. also this is interesting: https://stackoverflow.com/a/3783287/8313379

None of these answer my question which is valid - 2Gb leaked memory in 10 minutes still here.

Digika
  • 127
  • 2
  • 11
  • Those are both IIFEs. [The location of the parenthesis does not matter](https://stackoverflow.com/questions/3384504/location-of-parenthesis-for-auto-executing-anonymous-javascript-functions) (if this is the only code). There seems to be something else at play. – Bergi May 13 '19 at 21:00
  • That's the thing - I can only change the function wrapper syntax right now and the issue is back. It makes ZERO sense and that's why it is weird. – Digika May 13 '19 at 21:26
  • 1
    Try to make a minimal reproducible example of the injected code, and file a bug report against Chrome. I guess you'll find better help there than here on SO. – Bergi May 13 '19 at 21:34
  • Well, you talk about intercepting events and whatnot, and leaving event handlers uncollected is a cause of memory leaks... But the code you've shown shows, well not much to cause any memory usage, let alone a leak. – Heretic Monkey May 13 '19 at 21:43
  • You need to show `//my code here` – Lawrence Cherone May 13 '19 at 21:51

0 Answers0