JavaScript memory leak

I found by acci­dent that using some javascript func­tions in Direc­tor brings mem­o­ry leaks. Such a func­tion, for exam­ple, is Math.round()
To test this, cre­ate the fol­low­ing #score script:

-- "FRAME LOOP" script
on beginSprite( me )
end beginSprite

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

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

Cre­ate the fol­low­ing #movie script:

function round( n ){
  return Math.round( n );

The grow­ing of the used mem­o­ry is clear­ly seen in the mem­o­ry inspec­tor while the movie is run­ning. This can be observed also if the javascript func­tion is called by anoth­er javascript func­tion. The mem­o­ry leak is avail­able both in author­ing and pro­jec­tor mode. This is test­ed in Direc­tor 10.1 and 11.5 under Win­dows.


This is not real­ly a mem­o­ry leak, but the expla­na­tion is the fol­low­ing (from the “Undoc­u­ment­ed Lin­go” page of the “Direc­tor­fo­rum Col­lab­o­ra­tion Wiki”):

JavaScript Garbage Col­lec­tion

The JavaScript Inter­preter (Spi­der­Mon­key) remem­bers every­thing, until 8MB of sys­tem mem­o­ry are used. Then it starts to col­lect garbage. With the method gc() you can force garbage col­lec­tion any­time.

// JavaScript-Syntax

so if you want to avoid this “tem­po­rary leak” in your sam­ple code above, you could call _system.gc() e.g. after every 10000 frames.