Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-28-2008, 05:35 PM   PM User | #1
jaywhy13
Regular Coder

 
Join Date: Dec 2004
Location: Jamaica
Posts: 592
Thanks: 2
Thanked 0 Times in 0 Posts
jaywhy13 is an unknown quantity at this point
Profiling and Catching Leaks

I need some help conceptually I think to get rid of some javascript woes I'm having. Now I use this mapping tool called kamap. It's a fairly heavy javascript mapping tool like google maps. It's open source.

When the application loads up, the memory for Firefox jumps about 20-30 mbs. By right, when you leave the page... that 20-30 mb leap is supposed 2 disappear. They have a very basic example and it always comes back down. The memory is jumped by is reclaimed.

My application however, is now having a bit of trouble releasing the memory, so I know I have some leaks that I need to get rid of.

So I believe my questions are as follows:
1. How can I use tools like Firebug and it's profiling to rid me of leaks. For example, what exactly does the run time stipulate about a function? What information do they provide that can point me to a possible leak?

2. What's the best way to go about finding these leaks in the first place? Is watching the memory of firefox a good place to start? What tools are there?
__________________
I'm gonna find a way to download the internet if its the last thing I do...
Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished
jaywhy13 is offline   Reply With Quote
Old 04-28-2008, 07:31 PM   PM User | #2
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
use the delete keyword on your instantiated variables.

ie.
Code:
 
var myVar = new HugeObject();
//some code here
//...
//all done using myVar
delete myVar;
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Users who have thanked mjlorbet for this post:
oesxyl (04-28-2008)
Old 04-29-2008, 05:03 PM   PM User | #3
jaywhy13
Regular Coder

 
Join Date: Dec 2004
Location: Jamaica
Posts: 592
Thanks: 2
Thanked 0 Times in 0 Posts
jaywhy13 is an unknown quantity at this point
Can anyone help me out? I need some tips to ensure that I'm not doing anything wrong? How can I adequately check leaks.
__________________
I'm gonna find a way to download the internet if its the last thing I do...
Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished
jaywhy13 is offline   Reply With Quote
Old 04-29-2008, 05:15 PM   PM User | #4
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
add an onunload handler that deletes all your window scope variables, should clear any leaks up (as any variables declared inside functions without explicit window scope should be released on function termination anyhow)
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Old 04-29-2008, 07:17 PM   PM User | #5
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Please note: in JavaScript, the delete operator does not free up memory. What it does is remove a property from an object. In Microsoft's JScript engine, it doesn't even do that - it just marks the property as having been deleted. You should always null out the value of the property in JavaScript if you want to ensure there are no references to it that are keeping it live across garbage collections. Using delete may kill the last reference in some engines, but that is not a guaranteed mechanism, so don't use delete thinking that's what it does.

Oh, also, regarding mjlorbet's example in particular: delete can explicitly NOT remove variables declared with var. It can only remove properties from objects*, it CANNOT remove variables.

In other words:
delete myVar; --> myVar=null;




* Including the global object, as long as the property is not {DontDelete}, which it is if it was declared using the var keyword in global scope.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-29-2008 at 07:25 PM..
liorean is offline   Reply With Quote
Users who have thanked liorean for this post:
oesxyl (04-29-2008)
Old 04-29-2008, 08:56 PM   PM User | #6
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
it doesn't remove them, but it does set their value to undefined
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Old 04-29-2008, 09:23 PM   PM User | #7
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally Posted by mjlorbet View Post
it doesn't remove them, but it does set their value to undefined
Let me prove that it removes them:
Code:
var o={a:'a'};
alert('Does o contain a? '+(('a' in o)?'yes':'no'));
alert('Successful deletion of a from o? '+((delete o.a)?'yes':'no'))
alert('Does o contain a? '+(('a' in o)?'yes':'no'));

It does NOT change the value (and whether the value is changed or not is actually not visible to the script programmer, though it IS visible for the application programmer that use the scripting engine in his programs) - removing a property from an object does only mean that the property slot is no longer considered present the object. In some engines this may mean the property is actually removed. In some engines it may mean that the mapping from name to slot is removed. In some engines it may mean that the property is still there but explicitly marked as deleted so as to *appear* to not be there any more. That does not mean the property slot is actually removed from the object, or that the contents of that slot are changed. If a lookup for a property that does not exist on an object is made, the returned value is undefined, not the value of the property slot. For this reason, it may look as if the property has been set to undefined. That is not what actually happens though.

Edit: Oh wait, you were talking about variables here, not object properties. Never mind then. Let's prove that variables are not set to undefined instead.
Code:
<script type="application/ecmascript">
var
    t=true;
alert('Is t defined? '+((typeof t!='undefined')?'yes':'no'));
alert('Successful deletion of t? '+((delete t)?'yes':'no'));
alert('Is t defined? '+((typeof t!='undefined')?'yes':'no'));
</script>
This test will give a different result if run in eval however, because variables declared in eval are not set to {DontDelete}.

Edit: And just for my point about the distinction between properties of the global object and global variables, compare the result of my last code sample with the results of this code sample:
Code:
<script type="application/ecmascript">
t=true;
alert('Is t defined? '+((typeof t!='undefined')?'yes':'no'));
alert('Successful deletion of t? '+((delete t)?'yes':'no'));
alert('Is t defined? '+((typeof t!='undefined')?'yes':'no'));
</script>
Since a global variable t does not exist, a new property t of the global object is created. Since this is plain property of a normal object and not a variable, it is not {DontDelete} and in difference to the global variable, it may be deleted.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-29-2008 at 10:33 PM..
liorean is offline   Reply With Quote
Old 04-29-2008, 11:20 PM   PM User | #8
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
fair enough.
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:13 AM.


Advertisement
Log in to turn off these ads.