create wiki

go home go home
  1. about
  2. code
  3. wiki
  4. blog

V8/Garbage Collection

From Create Wiki

Jump to: navigation, search

v8's garbage collection is pretty transparent and simple. There are only a few functions relating to it. We only use one, but it is a vital one.

Contents

Adjusted Memory

v8 will mostly handle garbage collection for you. Sometimes, however, it might not handle it for you often enough. v8 collects its garbage when it thinks it needs to. In its decision making — do I collect now or later — It definitely takes into account how much memory it thinks it is using. After all, if it is not yet using much, why should it bother to collect?

But what if you have code like this in JavaScript:

for (var i = 0; i < 100; i++)
{
    var imageToModify = new Image("image-" + i + ".png");
    imageToModify.doSomethingInteresting();
}


v8 will think that the image only takes up a few bytes — after all, all it sees is its own representation. You know better, however: the image data itself is also taking up memory.

v8's designers thought of this. They had to. Code somewhat similar to this can actually occur in web pages, and web browsing is what v8 was originally designed for.

The function is a member of the V8 class, and you can call it like so:

//create the image
Image *image = new Image(path_from_javascript);
 
//tell v8 that you are using it
//note that V8 is a class in the v8 namespace. You could also
//address it as v8::V8::AdjustAmount...
V8::AdjustAmountOfExternalAllocatedMemory(image->getSize());


And when you are done with it:

//use the negative value to subtract
V8::AdjustAmountOfExternalAllocatedMemory(-image->getSize());


Fudging in Debugging

Sometimes — for instance, when debugging memory leaks or persistent pointers — it can be useful to force V8 to garbage collect more often. Although there is probably a more proper way, to do this, we just fudge the numbers we pass:

//create the image
Image *image = new Image(path_from_javascript);
 
//tell v8 that you are using it, and fudge the numbers
V8::AdjustAmountOfExternalAllocatedMemory(image->getSize() * 1000);
 
//use the negative value to subtract
V8::AdjustAmountOfExternalAllocatedMemory(-image->getSize() * 1000);

Context and Program Termination

v8 only collects garbage when it thinks it needs to. It does not do so when a context is disposed of, nor does it do so when the program exits.

This can cause some problems, especially if you have file output buffers that need to be flushed on program termination, or other such resources that need to be closed.

The only way to accomplish it is to keep track of the resources which need to be explicitly destroyed, and do so on program termination.

For an example of one way to do this, see this blog entry.

The Other Functions

There are other functions, mostly relating to allowing things to occur before and after garbage collection, and grouping objects for garbage collection. We don't use these, so I can't say much with any authority about them. If anyone would like to contribute, please mention it on the v8-users discussion group.

Personal tools