views:

167

answers:

6

I have written a program in Java which has 5 threads. In the run() I have a while loop which will loop over and over and this loop will loop a lot of times.

While the program is running, it is gradually eating ram bringing the program to a crawl. Is there anyway I can stop it eating all my ram?

Edit:
Actually just thinking about it, it probably is because the loop is creating lots of objects. Should I = null those objects at the end of the while loop??

+4  A: 

While the program is running, it is gradually eating ram bringing the program to a crawl. Is there anyway I can stop it eating all my ram?

Sure. Change your code so that it uses less RAM.

Michael Borgwardt
Great answer.... `:(`
jjnguy
@Justin, I think, Michael is just waiting for the OP to provide a few more details (or code) ;-)
Andreas_D
Why does this have +5? This is a ridiculous answer.
Platinum Azure
+1  A: 

Specific to @Michael Borgwardt's answer, try to minimize the creation of new objects. If you can re-use the same object instead of creating new ones every time, you can save some memory there.

But as others have said, without seeing your code, we're just guessing.

Paul Tomblin
What ultimately causes OutOfMemoryErrors is generally *not* the creation of new objects, but keeping references to unneeded old objects.
Michael Borgwardt
@Michael, He didn't say he was getting OOMs, he said it was slowing to a crawl. That implies to me that he's doing lots of garbage collection.
Paul Tomblin
Yeah, but current generational garbage collectors can churn through lots of short-lived objects without consuming significant resources. "Slowing to a crawl" typically happens when memory is truely running out and the GC has to work hard constantly to fulfill its "do everything possible to free memory before throwing OOM Errors" until the very end.
Michael Borgwardt
@Michael, like I said in the answer, without code we're just guessing.
Paul Tomblin
+8  A: 

If you are creating new objects and saving them in some collection in your loop that would easily fill up memory very quickly.

What you need to do is make sure you aren't saving any extra objects that you don't need.

Also, you should optimize the logic in your threads to consume the least amount of memory.

jjnguy
Now THIS is a real answer, even without enough information. (+1)
Platinum Azure
I love pithy remarks as much as the next guy, but yes, this is more helpful.
Tom
+1  A: 

It's normal for each one of your thread to have an infinite while loop, but simply having the while loops does not use up more RAM.

new Thread(new Runnable() {
    public void run() {
        try {
            while (true) {
                // do some work
            }
        } catch(InterruptedException ex) {}
    }
}).start();

You're doing something to consume the RAM, so you must debug your application and find the cause of the increased RAM usage.

Update:

You don't have to make the objects null at the end of the loop... if you're not holding on to a reference of those objects then the garbage collector should clean them up. What are you doing with the objects after you create them? How much RAM are you using up?

Lirik
+1  A: 

You might need a memory analyzer to understand what is "eating RAM". There are many tools available. One commercial tool is JProfiler.

A simple and free tool is to use Sun Profiler supplied with Java VisualVM. If you have JDK 6 installed you probably already have this program installed on your computer. Run the C:\Program Files\Java\jdk1.6.0_16\bin\jvisualvm.exe. Connect to the running Java process and press the Profiler tab and Memory button. You can now see what objects you have and how much memory they are using. Press refresh to update the view.

Lennart Schedin
A: 

Thanks for the help. It's definitely the Object causing it.

DRKM
Don't post a comment as an answer. Also, what Java version are you on? Java 7 (maybe maybe it was backported to 6) has got efficient object creation and disposal in tight loops of short-lived objects :) So, in the future (or in a better Java version) you might get away with it :)
Chris Dennett