JavaScript memory leak

I found by accident that using some javascript functions in Director brings memory leaks. Such a function, for example, is Math.round()
To test this, create the following #score script:

— "FRAME LOOP" script
on beginSprite( me )
_movie.puppetTempo(700)
end beginSprite

on enterFrame( me )
x = random( 1000 ) / 1000.0
retval = round( x )
end enterFrame

on exitFrame( me )
_movie.go( _movie.frame )
end exitFrame

Create the following #movie script:

//JAVASCRIPT FUNCTIONS script
function round( n ){
  return Math.round( n );
}

The growing of the used memory is clearly seen in the memory inspector while the movie is running. This can be observed also if the javascript function is called by another javascript function. The memory leak is available both in authoring and projector mode. This is tested in Director 10.1 and 11.5 under Windows.

 
Comments

This is not really a memory leak, but the explanation is the following (from the “Undocumented Lingo” page of the “Directorforum Collaboration Wiki”):

JavaScript Garbage Collection

The JavaScript Interpreter (SpiderMonkey) remembers everything, until 8MB of system memory are used. Then it starts to collect garbage. With the method gc() you can force garbage collection anytime.

// JavaScript-Syntax
_system.gc();

so if you want to avoid this “temporary leak” in your sample code above, you could call _system.gc() e.g. after every 10000 frames.

Cheers,
Valentin

Leave a Reply