Sign in to follow this  
QuackCoder

[web] Collect garbage with JavaScript

Recommended Posts

I am working on a project which works at 10 fps. Every time it repaints, it allocates more 4kb of memory.

Chrome has a function/button in its developer tools that allows me to manually collect garbage.
I am not aware if other browsers have this feature.

Is there a way to do this with JavaScript?

Share this post


Link to post
Share on other sites
[quote name='GHMP' timestamp='1313397684' post='4849297']
I don't think this question belongs in Game Design..maybe For Beginners or general programming?
[/quote]

Agreed.

To answer the question though, I don't think Firefox or Internet Explorer have any sort of garbage allocator that the user can interface with. What you need to do is find out why your code is allocating more memory like that. Is it allocating a lot of memory and eventually freeing it, or is it never freeing the memory and eventually crashing the browser?

If it is the latter, you most likely have a "memory leak".

This isn't the same sort of memory leak as you'd find in C, for example, because the garbage collector is supposed to automatically reclaim objects that are not referenced by any others. A common flaw in Javascript garbage collectors however is that they don't detect cyclical references and thus will never collect the memory for some objects. Consider the following code:

[code]
function do_stuff() {
var some_object = {
foo: 'bar'
};
some_object.ref = some_object;

// Do some stuff
}
[/code]

In this case, some_object has a reference to itself. Normally when it goes out of scope, the garbage collector will eventually remove it and return the memory to the system. However, since it has a reference to itself, the garbage collector thinks there is always some object referencing it and will never free it. I don't know if this sort of flaw still exists in modern browsers, but more complicated examples might still cause problems, such as this:

[code]
function do_stuff() {
var some_object = {
foo: 'bar'
};
var some_other_object = {
bar: 'baz'
};

some_object.ref = some_other_object;
some_other_object.ref = some_object;

// Do some stuff
}
[/code]

In this case, the two objects refer to each other and you have the same issue.

Share this post


Link to post
Share on other sites
[size="3"]So you mean that the variables which were defined are kept in memory since they are still referenced, right?

I just call a repaint method every 100ms. And in between, I move around a value from one index to another and assigning null to its last index.
So it's shouldn't allocate more memory, right?
[/size]

Share this post


Link to post
Share on other sites
You could try registering your object with a central manager and on each pain instruct the manage to null out any objects it has and to null out any referenced objects. In this way you may need to make your own pseudo garbage collector. You could also double check your use of "new" objects. Examine if you really need to new something/everything on each draw. See if you can't reuse existing objects and up their standing in the scope of your code to support this.

Share this post


Link to post
Share on other sites
[size="3"]No during repaint I don't create any new objects.
But in between due to user interaction I copy a value from array[x] to array[y] (same array) afterwards I null out the value array[x]. [/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this