Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

OutOfMemoryError

843810Jun 13 2006 — edited Jun 14 2006
We have a problem in our Swing application where we run out of memory.

What happens is, we have a number of objects in a model-view-controller architecture. The user pushes a button that indicates they want to throw away their changes and start again.

Originally at this point, nothing got cleaned up. Everything got created afresh, and the old objects stayed around, although you couldn't reach them; they were orphaned. Do this six times and you crash with the out of memory bug.

I put in code so that when the button is pushed, the main app calls a routine that theoretically ripples through all the controllers, all the views and all the models, empties all the collections and nulls out all the references. Then I put in a call to System.gc() and System.runFinalize(). I put in logging code to identify what objects get constructed, what objects get finalized, and what objects get cleanup called on them.

With my changes, some (blessed few) of the objects get finalized, but most don't. For the most part, the ones that don't are connected with the UI. So the main controller and its view and all the controllers and views contained within don't get finalized, although cleanup() was called for them and every place I've found references to them, I nulled them out. I can't find anyplace where I haven't made an attempt to decriment the reference count, but still the objects don't get garbage collected.

Nor are these objects reachable; the next time you hit them in the code, they're null (as I would expect them to be, since I nulled them out). So we have all these orphaned objects that aren't getting garbage collected, that take up space in memory, and eventually crash the program.

Would this have something to do with the UI? Do I need to remove components from the frame's content pane? I really don't understand why objects are still kicking around.

I considered reusing the old objects instead of creating new objects afresh, since I couldn't finalize them, but it put the application in a mixed state and that was a bad idea. I want to throw out what I had and start fresh.

I know that setting an object to null is only a hint to the garbage collector, but is there something I'm missing?

By the way. If your answer is what am I doing having more than one model, view and controller, all I can say is, hey, this is two-year old code (and a two-year old bug) that I've inherited. I can't rewrite the app from scratch.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 12 2006
Added on Jun 13 2006
4 comments
102 views