Garbage Collector for different targets

Before Haxe, I used ActionScript 3.0 and I would like to clarify with experts about the behavior of other targets. In a Flash, if an object does not leave the call stack, it is immediately removed from memory by the Garbage Collector. How does this work for other targets? It is especially interesting if this principle works for HashLink, C++ and JS.

1 Like

Generally, I would recommend not relying on “garbage collector” behavior. Various platforms do behave very, very differently in this respect. Instead, I suggest that you explicitly free everything that you have allocated – using runtime error traps to ensure that this always happens.

1 Like

I am aware of this, I just would like to better understand what is happening under the hood. Suppose how long an event instance will live in memory on different platforms after the method is completed, if the handlers receive only the reading of its fields and the link to it is not stored anywhere, i.e. he doesn’t leave the stack?

    function dispatch(arguments) {
        var event = new Event(arguments);
        for (handler in handlersList) handler(event);
    }

You don’t really have to worry about the gc, if you have a reference to the object it’ll be usable.
And for when it’s released it depends on the platform, for most it happens when you allocate memory or at regular interval.

2 Likes

Ok, I think I really worry about it in vain. Thx

hxcpp runs its garbage collection regularly when allocating, sadly no escape analysis here. For JavaScript it’s delegated to the JS engine which will probably do escape analysis and therefore delete the object right away. Haven’t had a closer look at the HL garbage collector yet.

2 Likes

This is exactly what I was looking for and thanks to you now I even know what it is called) Thank you.

In the code above, it would be desirable to explicitly discard the Event object after passing it to each of the registered handlers … using “try” logic to ensure that this happens even if an event handler throws a runtime error that isn’t caught. That sort of thing.

How can an object be discarded explicitly in Haxe?