1

I have some JavaScript Functions with source code coming from larger JavaScript-files. They are created this way:

const function = Function("foo", "...<large JS source code>...");

When looking at the memory snapshots in Chrome, the source code of these functions is retained and create a large memory overhead. Is it possible to "release"/"drop" source code of functions in JavaScript?

EDIT

I am really keeping the function itself around and wonder whether it’s possible to tell the function to drop the source code from memory.

user3612643
  • 5,096
  • 7
  • 34
  • 55

1 Answers1

2

Internally Chrome uses the V8 JavaScript engine. This engine is a just-in-time JavaScript engine which means that it takes your JavaScript and it compiles it to machine code when it needs it. This is expensive; so V8 will cache the result of the compilation and (in most cases) will re-use the previously compiled code if you call the function again.

I believe function memory reclamation is handled by the V8 garbage collector. When a variable (or function) falls out of scope (there aren't any references left to it anywhere, including closures) then the garbage collector is free to reclaim that memory including the source code and cached machine code. The garbage collector runs periodically and will clean up any memory from anything still in scope. Generally speaking you shouldn't try to force garbage collection on your own, it should happen automatically, but with Chrome there is a way to force garbage collection using the developer tools.

If you remove any references to your function (remember this includes closures) and force garbage collection you should see the memory reclaimed. Use the Chrome Developer memory tools to see if your function has been reclaimed or not (look at "heap snapshots").

There's one other caveat to this: even if the memory is reclaimed it won't necessarily be released back to the operating system or even cleared. Many applications handling large amounts of small memory allocations will try to improve performance by trying to re-use previously allocated memory before asking the operating system for more. So if you're using a low-level memory inspector you may still see your code hanging around in memory even if it's been garbage collected and there aren't any useful references to it. Without diving pretty deep into the V8 internals it probably isn't possible to determine from a memory dump if your code is still in memory because of a memory leak or because Chrome allocated the memory and simply hasn't released it back to the operating system after internally cleaning up references to that memory.

Michael Powers
  • 1,970
  • 1
  • 7
  • 12
  • If I understand correctly, the answer to your clarified question is no. Since JavaScript gets run though a just in time compiler the engine needs to keep the source around in the event it needs to be recompiled/reoptimized. You can't separate the source from the function object. – Michael Powers Oct 05 '18 at 21:30
  • Well, I thought that it keeps a tokenized/parsed version, but not the full bloated source... – user3612643 Oct 05 '18 at 21:36