This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Idle Simple Java Swing Application Memory Leaks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Idle Simple Java Swing Application Memory Leaks" Watch "Idle Simple Java Swing Application Memory Leaks" New topic
Author

Idle Simple Java Swing Application Memory Leaks

adann kuwenyo
Greenhorn

Joined: Mar 25, 2013
Posts: 2
I'm currently investigating a memory leak in one of our applications. After further investigation, I came up with a test of two simple java swing applications that sit idle for almost 14 hours. Both applications consist of 30 JButtons.

The 1st application is using a strong reference for its action listener:



The 2nd application is using a weak reference for its action listener:



Here's the WeakActionListener implementation:




I profile both applications using JConsole for 14 hours. I just leave them idle for that time frame. It shows that both applications either using weak reference or strong reference have an increasing memory heap consumption over time.

My question is, is this a bug in Java Swing API? What are the other alternatives in resolving this kind of memory leak?

Thanks in advance!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
It's only a leak if the memory isn't eventually reclaimed by the GC. Is that the case? If so, how did you ascertain that?


Ping & DNS - my free Android networking tools app
adann kuwenyo
Greenhorn

Joined: Mar 25, 2013
Posts: 2
Hi Ulf! Thanks for the reply. Yes that is the case. With my 14 hours of testing, the heap memory consumption starts from 2MB then went up to 5MB after 14 hours. As I said the application sits idle for this period. The monitor is off and the OS is locked. Also, the action listeners here only contains a System.out.println(). What do you think of the possible cause of this leak? Is it native in Swing?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
That's not an indication of a leak. The JVM has a lot more memory to work with than 5 MB, so the GC probably never kicked in. If it approaches its limit, and is then unable to free up memory - then you might suspect a leak.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
And welcome to the Ranch
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i seem to recall there is a way to specify the heap size when you run it using the java command
i don't remember how though


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Randall Twede wrote:i seem to recall there is a way to specify the heap size when you run it using the java command
i don't remember how though
Look here, and look for the -Xmxn option.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Idle Simple Java Swing Application Memory Leaks
 
Similar Threads
Click button is not function
NOT Abstract ERROR
What is the common structure for your ActionListeners?
ActionListener code. Problem deciphering of
How to make label visible