Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript memory usage management

I am building large website with heavy javascript usage, all of my content is being loaded trough ajax, it is very similar to facebook, and since there is a lot of different pages i need a lot of javascript, so what i thought of is to divide my script in to sections, each page would have it's own script file.

Now loading is simple, i just load a new file with each page, but my concern is what will happen if user goes trough 100 different pages and load 100 different script files?

At the moment my website doesn't have that many pages, but i'm quite sure it will grow to nearly 100 unique pages at some point in the future.

So what would happen to the user with slower computer? Im guessing it would start to slow down a lot since there would be no refresh. From what i have read it is impossible to just unload all events and data from loaded script file in any easy way, and if i were to try that it might cost me a way to much time and effort to do that.

So my question would be, should i just leave it the way it is or try to do something about it? I am currently using jquery with few plugins, and if i had to guess average file would be around 50-200 lines of code with mostly click events, and ajax calls.

Note, each page objects has it's own prefix for each class, for example: home_header, login_header

So there shouldn't be any conflicts between onClick event listeners and similar things.

EDIT I'm setting bounty on this question, i would like to hear more opinions.

like image 635
Linas Avatar asked Oct 07 '12 21:10

Linas


People also ask

Does JavaScript have memory management?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

How do I stop JavaScript heap out of memory?

Open the Start menu, search for Advanced System Settings, and select the Best match. In the Variable name field enter NODE_OPTIONS. In the Variable value field enter --max-old-space-size=4096. This value will allocate 4GB of virtual memory to Node.

Does JavaScript use heap and stack?

JavaScript engines have two places where they can store data: The memory heap and stack. Heaps and stacks are two data structures that the engine uses for different purposes.


1 Answers

Just because you are using AJAX doesn't automatically mean alarm bells with regards to memory usage... you should be more worried about the kind of things that cause memory leaks, and making sure you destruct as well as construct things properly:

  • http://javascript.crockford.com/memory/leak.html
  • http://nesj.net/blog/2012/04/javascript-memory-leaks/
  • http://www.ibm.com/developerworks/web/library/wa-memleak/

  • What is JavaScript garbage collection?

As a rule, in any large system I tend to create a helper constructor that keeps track of all the items I may wish to destroy at a later date or on page unload (event listeners, large attributes or object structures) all indexed by a namespace. Then when I've finished with a particular section or entity I ask the helper system - I call it GarbageMonkey :) - to clear a particular namespace.

  1. For events it unbinds
  2. For attributes it unsets
  3. For arrays / objects it scans and unsets each key and can do so for sub elements too
  4. For elements it removes and cleans content as much as possible

Obviously for the above to work you need to be wary about leaving variables lying around that can keep a reference to the data you hope to delete. So this means being aware of what garbage collection is, what closures are; and how between them they can keep a variable alive forever!! ..or at least until the browser/tab is destroyed. It also means using object structures rather than vars because you can delete keys in any scope that has access to the object, but you cannot do so for vars.

So do this:

var data = {}, methods = {}, events = {};

methods.aTestMethod = function(){
  /// by assigning properties to an object, you can easily remove them later
  data.value1 = 123;
  data.value2 = 456;
  data.value3 = 789;
}

Instead of this:

var value1, value2, value3;

var aTestMethod = function(){
  value1 = 123;
  value2 = 456;
  value3 = 789;
}

The reason being because in the above you can later do this:

var i;
for( i in methods ){ delete methods[i]; }
for( i in data ){ delete data[i]; }

But you can't do this:

delete value1;
delete value2;
delete value3;

Now obviously the above wont protect you from a reference that points directly to a sub element of either methods or data. But if you only pass the methods and data objects around in your code, and keep tidy with regards to attaching methods as event listeners, then even if you do end up with a rogue reference it should only point to an empty object (after you've deleted it's contents that is).

like image 186
Pebbl Avatar answered Sep 22 '22 05:09

Pebbl