Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 22nd, 2006, 10:36 AM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Memory leak in a loop

Hello

I have a loop, within which I call a method which creates an object. The object is referenced by a local variable only. When the method returns, the object goes out of scope and is available for garbage collection, if I've understood all this right.

But on the 73rd pass of the loop, I get an out of memory exception. 'java.lang.OutOfMemoryError: Java heap space', it says.

I've searched around, and all the advice I can get tells me two things. One is that I can't reliably summon the garbage collector into action. All System.gc() and Runtime.getRuntime.gc() do is suggest that now is a good time to deploy the garbage collector, but they don't force it to run. And they don't help anyway as it turns out in my case.

The other thing I keep seeing is that I must make sure my objects really are available for collection when I've finished with them. And I'm pretty sure they are, because of this: if I put an extra line inside the loop, which simply puts a dialog on the screen and waits for the user to acknowledge it, the leak doesn't happen, the loop can execute indefinitely and no memory problems get reported. So it seems to me the garbage collector takes the chance to run while the dialog is up, and it is successfully collecting my dead objects, which are therefore correctly dereferenced when I think they are.

I'm being a bit long-winded, but I'm hoping someone can tell me if they agree with me that my objects do indeed seem to be available for garbage collection, and that putting the dialog in the loop really does allow the garbage collector to do everything it needs to do. Or have I misunderstood the issues here?

I don't want to make more memory available because that would just get me round the loop more times I think, before the leak gets reported. It wouldn't solve the problem of the garbage collector just not deploying when I need it to.

Can anyone help me solve this?

Thanks
Neil
Java 1.5 on Windows XP Home

 
Old March 29th, 2006, 10:25 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey Neil,

You're right! You never know when the garbage collector will run, which is to say it runs asynchronously, in another thread.

I'm inclined to think that there may be another problem here, though. You'd expect waaaay more than 73 objects to get
created before your stack ran out! Can I see the code?

In the meantime it might be worth checking out about object finalization, cf:
http://java.sun.com/docs/books/tutor...m/garbage.html

Hope that helps,
Charlie

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
 
Old March 30th, 2006, 06:34 AM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Charlie

Thanks for your reply, and thanks for your confirmation that I've understood what's going on correctly at least!

I take your point about 73 objects being a very small number that you wouldn't expect to bring the system to its knees. However, the object has a large number of other objects as members, each created anew again and again inside the loop, and each of these presumably has some overhead.

I've actually solved the problem now by just creating the object once, outside the loop, and then inside the loop I reallocate all its members each time to ones that already exist, instead of creating fresh ones. This is probably a better way to go about things in general. It certainly got me out of trouble this time.

Thanks for the link about finalization too. I wonder if calling System.runFinalization(); System.gc(); would have worked in my case. Is it possible it might have worked on my hardware but not someone else's? Processor speed comes into it, according to the page you've linked me to. Anyway, happily I no longer need to find out :-)

Thanks Charlie. It helps a lot to talk about this to people who know what goes on inside the computer, under the covers.

Neil

 
Old August 14th, 2006, 06:08 PM
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had found, a few years ago (1.1), that explicitly setting the object equal to "null" in the code seemed to help the garbage collector. I don't know if Java 1.5 fixes this bug or not.

Rick
 
Old March 17th, 2010, 05:59 AM
Registered User
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Dictionary Manager (UDM)

Hi neilsands,

Can i contact you about your issue with the User Dictionary Manager (UDM) for Android?

Thank you,
BR,
Adrian Vintu





Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for Memory Leak Advice Bill_Thompson Visual C++ 0 October 16th, 2007 05:42 PM
IE Memory Leak Jman82 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 July 6th, 2006 05:22 PM
Memory Leak in ADO Seeja Pro VB Databases 1 December 27th, 2004 07:28 PM
Think I have a memory leak, need advice grantmeans Classic ASP Databases 1 March 12th, 2004 03:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.