2

I've been using javascript module pattern for while.

I showed an example of a module pattern to one of my coworkers. He said that the following code can make a memory leak.

var test = (function(){

   var events = {
      // my functions go here
   }

   return {
     // return something
   }

}());

he said that since events variable is an object and i'm not setting null for it, it can cause a memory leak even if I set null for test later.

As far as I know, the above code snippet is okay because I'm not passing events around.

I need advices!

2 Answers 2

3

I think your code is fine. Memory leaks in closures happen when you have a reference to a DOM element, because of the circular reference (foo holds on to element, and element holds on to foo, so they can never be garbage collected):

function foo(element, a, b) {
    element.onlick = function(a, b) { /* uses a and b */ };
}

My source for this belief is here

But others might know of another problem ...

Sign up to request clarification or add additional context in comments.

Comments

0

@moon - Why worry too much? How often is that code getting executuded between sittings? How many bytes are going to get lost? Then you have to consider the various browsers that will interperet and run your Javascript.

In short - concentrate your efforts elsewhere - then you get better rewards.

8 Comments

// um...I put that code sample not because I'm using that code. That's just an example as I mentioned on my post.
@moon - I just think that your efforts will be better served elsewhere. (BTW - Name from Enid Btyton - Folk of the far away tree)
// i appreciate and understand your intention here, but a memory leak is not something that I want to skip; concentrating my efforts elsewhere by leaving a known problem is a problem in my humble opinion
@moon - How big is that memory leak? A few bytes? Perhaps better to look at the server end and dish out some better cavar?
// as I mentioned...this is..just a sample snippet code. Let's assume that the above code causes a memory leak. If I keep using that on my all projects, that would be a problem. Again, I understand what you mean...but I'm a learner here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.