wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes finding and fixing memory leaks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "finding and fixing memory leaks" Watch "finding and fixing memory leaks" New topic
Author

finding and fixing memory leaks

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 919
    
  13

I've struggling with some "over the fence" code that is leaking memory badly. I've been through the scriptlet code (ugh, I know) and I've fixed some of the memory usage, but it's still way out of whack, so I'm now looking at the related Javascript. While reading a page on identifying and fixing leaks, I found this nugget:

"Fortunately, finding jQuery memory leaks is easy. Check the size of $.cache. If it’s too large, inspect it and see which entries stay and why."

Sounds simple enough, but the page doesn't explain how to do this. How to inspect? What is "too large"?

Can anyone guide me on this?

Are there any third-party tools designed to identify memory leaks?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Which side of the fence is the memory problem on? Server or client? If the server, looking at the JavaScript code isn't going to help. If on the client, looking at the JSP server-side code wasn't helpful.

What are the details of the issue?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 919
    
  13

It's on the client side. We have what we call a Slide Show application. It displays pages in kiosk mode in a constant rotation. This app has been in use for several years and it runs pretty clean. Restarting the systems once a week keeps them clean enough to avoid IE errors.

This latest page they've sent me runs in a loop with 3 different sets of parameters (displaying 3 groups of machines). The IE memory usage starts at about 40k when the slide show first loads, but with every page load the memory footprint (via task manager) increases by at least 10k. Within an hour or two, IE is consuming over half a gig of memory. If I leave it running all night, it will be locked up by the time I get here in the morning. The CPU utilization is still negligible, but IE has consumed pretty much all available memory.

I've tested it with a program called sIEve, and it says "leak, leak, leak" like crazy, but I'm having a difficult time identifying what it's complaining about. Now I've switched to the profiler in the Chrome Developer Tools and following a tutorial for that, but this is challenging, to say the least.

The Javascript for the page runs a timer that refreshes the page automatically and I suspect the problem is going to be somewhere in that code, but it's not my code so I'm working through it slowly and trying to understand it. There are several hundred lines of JS so I hesitate to post it here, but I can post the functions related to the timer if you think that will help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Bear in mind that testing in chrome may not help find leaks that may only manifest themselves in IE. IE -- especially older versions -- has leak problems of its own. Do you experience the problem in all browsers or just IE?

Some of the biggest culprits for memory leaks in JavaScript are expandos placed on elements that refer to other elements. As elements are created and destroyed, closures can create circular references that prevent garbage collection. This is one of the reasons it's best to tack extra data onto elements using jQuery's .data() rather than expandos -- it's stored in such a way that circular references can be avoided.

Of course, there are a bazillion other things that can go wrong too.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 919
    
  13

Good point about IE. This is XP Embedded, so I'm stuck with IE 7. I'll try Firefox or Chrome and see if it's any better.

I'm going to post some of the code that I think might be relevant. Anything here jump out at you? Sorry for the length. I've removed everything that I think is not relevant.

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2342
    
  28

You know long long time ago before we had nice memory analysis tools that showed you howmany objects were allocated, I had come up with a trick to identify memory leaks. I called it "finding memory leaks using binary search". SImply put, I would comment out half the functionality and test the app the see if memory usage increases. If it does, the problem is in the code that isn't commented out. If it doesn't, the problem is in the code that is commented out. Take the code that has the problem, comment out half of that and repeat till you find the culprit. Many times, the problem is in multiple places, and this helps you find one place. So, once you fix the problem, you have to go back and repeat the whole process again.

It's an exhausting way of solvin memory issues. However, when tools fail you, you have to just grin and bear it. Also sometimes, disabling parts of functionality changes the way other code behaves. So, you have to mimic the functionality that you comment out
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 919
    
  13



Thanks, Jayesh. As soon as I read your post I realized that I don't need all that timer code. This page was designed to be left open on a desktop so the auto-refresh was needed. But the way I'm running it as a slide show, the page will be reloaded every 45 seconds, so there is no need for it to auto-update. Good thing too, because this memory profiler is kicking my butt.

Time to start commenting out code...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding and fixing memory leaks