Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swings, Event Listeners & OutOfMemoryError: Solutions?  RSS feed

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a 2 Panes; lets call em Main & ComboBox pane; ComboBox pane has a JComboBox with an ItemListener registered to it;

Based on the JComboBox selection I make a database query; I use the query results to populate a JTree & JTable combination;

I have a TreeSelectionListener registered with my JTree;

Needless to say I have to rebuild my Tree & Table based on JCombobox selection; But before I rebuild the tree & table; I remove all the rows from JTableModel; remove all nodes from the tree and also remove the TreeSelectionListener registered with the Tree

Inspite of deregistration of Listeners I hit a "java.lang.OutOfMemoryError" after 3 or 4 selections in JCombobox.

Initially my JVM uses about 48M; after I click on ComboBox Pane the memory consumption increases drastically to around 100M and keeps on adding about 0.5M for every selection in JCombobox; Garbage collection brings back the memory usage to acceptable levels of 30M

I know by increasing JVM heap size using: -vmargs -Xmx128M & I am delaying the inevitable But I'm sure there are better ways to get around the problem.

Please do recommend ways on getting around the problem?
Is System.gc() advisable after every item Selection?

TIA
Ravi
[ September 29, 2005: Message edited by: Ravi Sathish ]
 
JigaR Parekh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to use jprofiler and see is there any referances which is bounding objects.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing forum.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is System.gc() advisable after every item Selection?


It is my understanding that a garbage collector is essentially required by the specification to do all it can to reclaim resources before throwing an OutOfMemoryError. That being the case, System.gc() which merely suggests that garbage collection should't cause anything to happen that isn't already being attempted.

You haven't posted any code, that might help. You say garbage collection causes it to return to 30mb, how are you causing this and how are you monitoring it? If you invoke garbage collection yourself does this keep it from happening? It sounds like you're leaving a reference to your data somewhere, causing it to not be collected.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something I just read by Tony Morris in the advanced forum struck a chord with me.

You start a VM with 256MB heap, and during operation it averages about 80-100MB heap, until it gets to one certain piece of resource intensive code that attempts to allocate 200MB, which of course, results in a OOME. What do you do? Shut down and die? What if you wish to recover?


How much data are you trying to load? I'm no expert on garbage collection or memory management. That being said, if you're trying to allocate more memory than you have available because what you're getting from the database is particularly large, that could be the source of the problem. If you're using 100mb with 128mb heap and 70mb of that is eligible for collection, I don't know if trying to allocate 50mb will cause an OOME or if it will collect first, then try to allocate.

Either way, how much data are you loading and how exactly?
 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jigar & Ken,

I used JProfiler to get a memory snapshot & found out that the problem was due to a stupid byte array creation.

About bottleneck in code:
I have a method A with for loop with an average of 10 iterations invoking a method B with while loop. This while loop has on an average 10 iterations again & invokes another method C with another while loop. Inside this loop I was loading objects into the memory with a byte[] of 2MB instead of 256 bytes - 1K;

I took care of this problem & my application runs consuming 20-30M

Invoking Garbage Collection & Monitoring Memory Allocation:
I used to rely on JVM to invoke GC. I relied on Windows Task Manager for monitoring until Jigar suggested JProfiler.

Ravi
[ October 04, 2005: Message edited by: Ravi Sathish ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!