Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am reading lot of articles regarding memory leaks. But i am not sure about below code. I usually do coding in this way. Can anyone tell me, will they leak memory?

I have doubts in following 3 scenarios (marked them in code).

I got these suspects after reading this post

var orderDetails = $("<root/>");

function GetOrderDetais()
{
    $.ajax(
    {
     url: url,  
     dataType: 'xml', 
     data: someThing,
     dataType: 'text', 
     type: 'POST',
     contentType: "text/xml; charset=utf-8",
     success:function(value) //1 - closure function refrencing global variable
     {      
        orderDetails = $(value);

        orderDetails.find("header").each(function() //2 - closure function refrencing global variable
        {
            //loop through order header details and do some DOM appends here.
        });

        ShowOrderDetails();
     },
     error:function(jqXHR, textStatus, errorThrown)
     {
        alert("Error" + textStatus + " ==> " + errorThrown);    
     },
    });

}

function ShowOrderDetails()
{
    var orderNum = orderDetails.find("header oNum").text();

    $(orderDetails).find("item").each(function() //3 - closure function refrencing outer variable
    {
        //loop through item details and do some DOM appends here to form table.

        //one intresting statement.
        $("div").append($(this).find("itemNum").text()  + " - " + orderNum);
    });
}

Update:

See this Video to learn about memory leake.

share|improve this question
    
There is no such thing a memory leaks in JavaScript. However there can be memory leaks in JavaScript implementations of browsers due to bugs. But these are very different from browser to browser - even between minor versions. Usually a JS developer doesn't need to worry about memory leaks, unless they have error reports about memory problems in a specific browser - and even then these are usually fixed quite quickly by the browser manufacturer (except older IE versions). Do you have concrete memory/performance problems in specific browsers? If not, you shouldn't worry about it. –  RoToRa Dec 17 '12 at 11:58
1  
@RoToRa. No. javascipt can be suffered by memory leaks due to poor coding. There are lot of articles available in internet. –  user10 Dec 17 '12 at 12:16
    
Show me one. I think you (or the articles) are not referring to memory leaks, but inefficient memory usage - a completely different thing. –  RoToRa Dec 17 '12 at 13:48
    
@RoToRa check out this link –  user10 Dec 18 '12 at 4:53
add comment

2 Answers 2

No, there is nothing you're doing in that code that would result in memory leaks.

I found an article written by Jack Slocum (creator of the ExtJS framework) that details some basic steps to follow to avoid memory leaks. The original article is a bit dated and browsers have gotten a lot better at cleaning up memory, so I don't know how much of it still applies to the latest browsers (it certainly applies to older versions of IE and Firefox).

Here is the article: http://maxthonuser.blogspot.com/2008/02/easy-steps-to-avoid-javascript-memory.html

Relevant text (in case the link ever dies):

It's as simple as these 3 steps:

  1. Never put anything in a DOM expando or property other than a primitive value unless you plan on cleaning it up. This is the most important rule of all. It may seem convenient to put your JS object in a DOM expando, so you can $() and get it, but don't do it. Sure, I know what you are thinking, I am being a little paranoid. There are lots of instances where putting a JS Object in a DOM expando won't cause a leak. That's true, but there are also many that will... some which are not so easy to detect (i.e. closures). So to avoid the possibility all together, I follow this simple rule.

  2. Clean up all your DOM event handlers on unload if there's a chance they could reference a DOM object. There's no reason to manually do this when there are libraries that do it automatically. I use YAHOO.util.Event for all my event handlers, it handles this for me automagically. Other libraries (prototype, dojo, etc) have some sort of mechanism to do the same thing, although I'm not sure how effective they are. If you look at the leak images above once again, you will notice almost all of them are in event related code of those libraries.

  3. Set your XMLHttpRequest onreadystatechange handlers to null when you are done with them. I use YAHOO.util.Connect for all my XHR connections and it uses a polling mechanism instead of readstate, so I don't need to do this anymore. If you can switch to YAHOO.util.Connect (or YAHOO.ext.UpdateManager built on top of it), I'd recommend it.

share|improve this answer
add comment

I can't find any reason for your global variable.

As far as I can see, it is declared and overridden in the success handler of your ajax request. So why not declearing it inside the success handler?

Next:

Why didn't you use a parameter in

function ShowOrderDetails(orderDetails)

so you could pass your orderDetails easily from your success handler to the function. No reference to a global object needed - or better: no need for a global object at all.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.