Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Idle Simple Java Swing Application Memory Leaks

 
adann kuwenyo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
adann kuwenyo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic