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 begin­Sprite( me )
end beginSprite

on enter­Frame( me )
x = ran­dom( 1000 ) / 1000.0
ret­val = round( x )
end enterFrame

on exit­Frame( 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 Windows.


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 Collection

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 anytime.

// 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.