Wednesday, October 12, 2011

Memory Management - JavaScript

avoid memory leak setting This = null ?
Hello Glen,
thanks for sharing your knowledge with us, this was pretty much helpful.
What about the memory leak?
Would it be enough if i set all properties of an object to null, the object itself to null and at least This to null? Would the garbage collector be able to destroy my original object and also the reference This?
Thanks+regards,
Christoph

Last edited Feb 9, 2009 12:41 PMReport abusive comment
+1Hide replies to this comment ▲
Hi Christoph,

That sounded like a good plan, so I put it to a test. I created an object with "var This = this" in it, then a destroy method that set This = null (you shouldn't be able to access that closure from outside the object, so you need a destroy method).

The HTML page itself created 500,000 instances and stuffed them into an array (window.cache); then I looked at the memory delta. Then the HTML page deallocated everything by calling the method that set This = null, and also set the window.cache reference to null, just as you suggested.

The results in Firefox 2 were promising. The initial memory consumption was about 43K, then it shot up to 373K. After deallocation and waiting a few minutes, the memory was back down to 86K. So not a total prevention of the memory leak, but not bad.

In IE7 and Chrome, the results were less promising. IE7's memory consumption shot up to 910K (!) and only came back down to 744K, and the tests were inconsistent as well. Collection seems to be immediate in IE but the memory leak is gigantic. I even added a line to set window.cache = null and this may have improved the result somewhat, but I was still left with 583K consumed. (The next test brought it to 374K. Weirdly inconsistent.)

Chrome never reclaimed memory, and I waited around about 20 minutes for the collector to run. However, it only ever consumed ~83K total memory (meaning it needed about 44K for those objects).

The good news (sort-of) is that both Chrome and IE reclaimed all the memory when I navigated to google.com after the test. (Firefox did not.)

Certainly an interesting test, but the end result is that (especially with IE) it seems that you can't get much of the memory back in a fully Ajaxian situation -- you have to navigate away at some point, or performance will certainly degrade.

But, like I said in the post, the vast majority of applications won't create that kind of consumption, so reclaiming closures isn't a huge issue most of the time.

Wednesday, March 23, 2011

Hero

Flying around discovering powers as a prank. Using them to stealquickly, tryin to get away with it. Getting closer and closer to being cornered by the police. Getting discovered by the police due to using technology blogs/microfeeds that are in correlation with acts of crime. Kidnappg/hurting friends and family.

Friday, February 18, 2011

Protagonist Antagonist Video Game Story

User controls a protagonist to over come antagonists in video games. However while the protagonist awakens to a new world he willing fights enemies to start out of ignorance. However at every defeat, the user does not care for our fallen protagonist. Instead, it is the antagonist, or various enemies in the game who help bring our protagonist back. Our antagonists are the ones whom teach the protagonist and help the protagonist grow, progression only at each of their deaths. Reluctantly the protagonist not knowing why must kill all antagonists. Those who are friends, mentors and teachers. Prophecy of wolf defeats bear, protagonist much change and become a werewolf to bite bear and win. In the end, the protagonist cannot overcome and get close enough to finish the bite. Bear realizes this and knows protagonist will never win, turns around and offers their self so that the protagonist can progress, knowing otherwise both will be locked in this struggle for an eternity and, having become friends, does not wish this to happen. While user is disconnected, they joke, talk, have their laughs. User takes control. Protagonist doesn't want to kill antagonist, but for some reason knows he must. Protagonist walks up to antagonist, embraces, and while crying, bites. Antagonist explodes into a thousand stars, the protagonist has progressed again.

The protagonist was young and naive, with no experience just waking up in this world, believing he needed to defeat all antagonists. Antagonists, knowing of the incoming aggression, defended themselves accordingly, but while defeating protagonist, always brought him back out of compassion since he was one of their own - just another sprite/avatar in the world. Coming back multiple times, protagonist develops a memory, but doesn't know what drive is forcing him to progress and kill all of the antagonists. He doesn't know why they help him, why they set themselves up for death. There is no guilt, only sadness, since this is the activity all their lives entail. Each antagonist challenges him, forcing him to learn and become stronger to overcome. They are kind, helpful, and push him along further and further. And when they're done, the protagonist is forced to leave them behind.