Is flash.utils.setTimeout () a memory leak if clearTimeout () is not called?

The ActionScript 3 documentation for flash.utils.setTimeout states:

If you do not call the clearTimeout() function to cancel the call to setTimeout() , the object containing the timeout close function will not be garbage collected.

That sounds funny. There is no reason to believe that it should contain a reference to a function / close that it will never call again, and equivalent JavaScript functions do not leak memory .

It's true?

+4
source share
1 answer

This is not true, at least in the current version of Flash.

I created a function that creates a large string, creates a closure by referencing it, and then calls setTimeout with the closure.

 public function tick():void { var data:String = "helloworld"; for (var j = 0; j < 20; j++) { data += data; } var f = function() { var i = 0; if (data.length > 0) { i++; } }; setTimeout(f, 0); } 

I used this feature very often and used System.totalMemory (as well as the OS X activity monitor) to monitor flash usage.

 function Main():void { setInterval(tick, 10); setInterval(display, 500); } public function display():void { trace("Memory usage: " + System.totalMemory + "B."); } 

Memory usage would not last long, but would then fall down when the garbage collector jumped. Average memory usage was stable for several minutes.

When I changed the code to keep a clear link to each close 1 memory usage increased to a few gigabytes in a couple of minutes. The documentation is out of date. Flash does not support closure references passed to setTimeout . (At least on OS X, I assume this will be the same on Windows.)

1 I created the array as a static class var and push ed each f on it before calling setTimeout .Sub>

+5
source

Source: https://habr.com/ru/post/1480167/


All Articles