

How about a simple "onmemory" event that gives js apps a cleanup chance? - javajosh

How about a simple "onmemory" event that gives apps a chance to clean up unused objects before crashing the VM? Good heavens why hasn't this been done yet? (Inspired by this comment: http://news.ycombinator.com/item?id=4527782)
======
se85
In most cases this event wouldn't be too useful because:

a) More often than not, these errors are caused by objects which could not be
removed by the Garbage Collector because they are still within an active
scope, so by definition the objects consuming all the memory are still very
much in use. A better understanding of JavaScript would go much further than
an onmemory event to try and monkey patch the memory leaks away.

b) There's no guarantee that you could clean out the objects before the VM
crashed anyway, because the event fired would be asynchronous, and the
JavaScript isn't going to halt everything else and wait for your code to run.

I know there is plenty more reasons as well when you get deeper into the GC
collection side of things - but I won't go there without narrowing it down to
a particular javascript platform first ;-)

Javascript being able to hook into things like this so you know more about
memory usage and stuff would be great, but there is a number of technical
hurdles to overcome, and I doubt that most Javascript developers would care of
its existence if all the work was done to make it so.

------
JohnHaugeland
There's no form of explicit cleanup in JavaScript, and if you're able to clean
up implicitly at that event, you already should have.

That's why it doesn't exist. There's nothing you could do there meaningfully.

