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?
[web] Collect garbage with JavaScript
I don't think this question belongs in Game Design..maybe For Beginners or general programming?
I don't think this question belongs in Game Design..maybe For Beginners or general programming?
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:
function do_stuff() {
var some_object = {
foo: 'bar'
};
some_object.ref = some_object;
// Do some stuff
}
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:
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
}
In this case, the two objects refer to each other and you have the same issue.
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?
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?
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.
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].
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].
Do you pass the value directly or do you have it wrapped in an object? My thought there is that instead of the browser passing the reference around to the data it's copying the data instead.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement