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?
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 - updated with new look and Ping home screen widget
Joined: Mar 25, 2013
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?
Joined: Mar 22, 2005
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.