Out of memory.
Does anyone know what could cause the following exception? I get this exception as soon as I run my application. I am using JRE 1.5.0_8. The problem is, some times I get this exception and some times not.
Exception in thread"Thread-52445" java.lang.OutOfMemoryError: Java heap space
Exception in thread"Thread-52452" java.lang.OutOfMemoryError: Java heap space
Exception in thread"Thread-52457" java.lang.OutOfMemoryError: Java heap space
Exception in thread"Thread-52459" java.lang.OutOfMemoryError: Java heap space
Exception in thread"Thread-52488" java.lang.OutOfMemoryError: Java heap space
Exception in thread"Thread-52499" java.lang.OutOfMemoryError: Java heap space
Exception in thread"Thread-52586" java.lang.OutOfMemoryError: Java heap space
> Does anyone know what could cause the following exception?Yes. You've run out of heap space. Your program is doing something which consumes a lot of memory.
I can add to this an experience I once encountered. I noticed once my VM increasing a lot but the physical memory stayed the same. Any idea? I know the question is too broad, but I am trying myself to narrow it down.Thanks,
Get a profiler and watch the memory usage. That'll tell you what's eating it up.
Can you at least give a hint on what kind of work your application is doing?
The other problem is I can't reproduce it easily. I am thinking my Java is going out of sync of some sort and losing it track of heap usage.
> The other problem is I can't reproduce it easily. I> am thinking my Java is going out of sync of some sort> and losing it track of heap usage.Is this more likely than you having a bug?
It抯 mostly GUI. I have a bunch of threads that run at the beginning and continue running until the GUI is ready to use by the user.
> The other problem is I can't reproduce it easily. I
> am thinking my Java is going out of sync of some sort
> and losing it track of heap usage.
Then you need to focus on being able to reproduce the problem. Until you know what triggers the memory leak, you can't fix it. It's almost certainly a bug in your code, not a bug in the vm.
Possible. But hard to find since I can't reproduce it.
It could be caused by so many different things. Unless you want a very generic answer, we need more information.
The generic answer would be that the JVM has ran out of allocated memory. What caused it we cannot tell by just the exception.
You might be able to control it by using the -Xmx parameter at the command line when you run it, but my guess is that your code is doing something so that doing this won't help any. I'm guessing that there is some kind of loop where it is creating objects or recurssive calls where it is using up all available memory and that no matter what value you use it will continue to use up all available memory.
> It抯 mostly GUI. I have a bunch of threads that run> at the beginning and continue running until the GUI> is ready to use by the user.How does this structure result in Java 'going out of sync' ?
I do have a profiler but without reproducing, it is useless. Could this happen, if one closes the application and restarts it very quickly. Would the JVM in this case have a problem?
> I'm guessing that there
> is some kind of loop where it is creating objects or
> recurssive calls where it is using up all available
> memory and that no matter what value you use it will
> continue to use up all available memory.
I agree that increasing the memory won't solve the problem if there is a memory leakage. The memory leakage is however probably not related to a recursive loop since you usually get a StackOverflowError before you get an OutOfMemoryError.
kajbja at 2007-7-12 19:52:18 >

> I do have a profiler but without reproducing, it is> useless. Could this happen, if one closes the> application and restarts it very quickly. Would the> JVM in this case have a problem?No
kajbja at 2007-7-12 19:52:18 >

In normal cases the application consumes up to 67M at start up and I do have as you said a -Xmx of 300M.
> In normal cases the application consumes up to 67M at> start up and I do have as you said a -Xmx of 300M.Do you manipulate large images?
kajbja at 2007-7-21 22:30:33 >

No.Jjust small icons which end up darwing a structure of hard ware.
I'd start by looking into what creates the threads. From what little you posted, I would guess that your app is creating well over 50,000 threads, which is a lot of threads for any app. and could consume a lot of memory depending upon how large each object is.
This is what I don't understand. I don't have that much of threads. Probably I have up to 10 threads runing at the same time at start up.
Maybe you're swallowing an exception and thus walk into an infinite loop where you create more and more threads ...?
Going back to the heap size, as far as I understand when the heap increases the RAM will increase. Is this correct?
Ok. In your original post a line says:
Exception in thread "Thread-52445" java.lang.OutOfMemoryError: Java heap space
Any idea to what the "Thread-52445" refers? Is it a name of a thread? Do you name your threads according to the number in which they were created?
Very possible cause. I will have to double check to see if I have this situation.
Available RAM to the app., yes. But, if you have some kind of infinite loop that is creating objects it will consume all the "RAM" you give it.
>Ok. In your original post a line says:
>Exception in thread "Thread-52445" java.lang.OutOfMemoryError: Java >heap space
>Any idea to what the "Thread-52445" refers? Is it a name of a thread? >Do you name your threads according to the number in which they >were created?
No, I have no idea. I don't name my threads. If I name them though, would java print the exception with correct names or would it print the threads IDs?
>Available RAM to the app., yes. But, if you have some kind of infinite >loop that is creating objects it will consume all the "RAM" you give it.
I once noticed an exception very similar to this but in That one I was able to see what happened to the memory. I had the application at -Xmx300M and when started the RAM for this application was at 68M, which is normal but its VM size increase like crazy to over than 300M, which is above the requested size, and it crashed. Put in mind I have 3G of RAM on my machine when it happened.
Thanks,
I would just like to add that my application does talk to hardware via serial port and TCP.
If you haven't already, try increasing the heap by using the java command option -Xmxnm, where n is the heap in MB. Assuming you are using Win 32 XP, you'll find that your maximum value will be somewhere between 1200MB and 1700MB (approx.) This is a limitation of Windows, not much can be done to change it.
You can use this command with varying values of MB to find your maximum:
java -Xmx1330m -version
Here is another pattern of exceptions which I got before it crashed.
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15121" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15126" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15205" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15222" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15283" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-8" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15304" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15392" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15422" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
Exception in thread "Thread-14406" Exception in thread "Thread-76" Exception in thread "Thread-15425" java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15424" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15177" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15210" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15312" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14573" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15430" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-156" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15434" java.lang.OutOfMemoryError: Java heap space
Exception occurred during event dispatching:
Exception in processing network message message.Connect
Exception in thread "Thread-77" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14110" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15454" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15442" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15452" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15418" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15449" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14268" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14505" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14474" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14681" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14599" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-15457" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14436" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-14694" java.lang.OutOfMemoryError: Java heap space
>If you haven't already, try increasing the heap by using the java >command option -Xmxnm, where n is the heap in MB. Assuming you >are using Win 32 XP, you'll find that your maximum value will be >somewhere between 1200MB and 1700MB (approx.) This is a >limitation of Windows, not much can be done to change it.
>You can use this command with varying values of MB to find your >maximum:
>java -Xmx1330m -version
The machine where the application will run has 512M. What happens if I put my request (-Xmx) at a value greater than 512M?
I just did a little test, and the default name for the thread is "Thread-n" where n is the number of the thread that was created, so from the looks of it, you have over 50k threads in your original post, and in this one, there are over 15k threads.
What I would do is some old-fashioned debugging and put in some System.out.println()'s in the location where the threads are being created, or some logger.info()'s, if you are using logging. I would also put in a lot of System.out.println()'s in several other key locations with whatever information that would help you figure out from where that print is being printed, and what is happening at that location.
Would n be the number of threads alive or the number of threads that have been created so far including the ones stopped and collected by the GC?
Suppose you had 6 threads called Thread-1, Thread-2, and so on up to Thread-6 in the JVM that you designed. Then Thread-2 terminates. What do you do, as a sensible JVM designer? Do you rename Thread-3, Thread-4, and so on? Or do you just leave them alone, suspecting that people might get confused if you changed their names?
A lot of questions can be answered by thinking about what somebody with some common sense would do.
I believe that it is the number of threads that were created. Whether they are all still alive or not may not matter. I don't know for certain without doing a test first, but that is my guess.
If you are creating 10 threads, killing those 10 threads and creating 10 more, etc. 5,000 times, your GC might not have a chance to collect all the garbage and you could still run out of memory, but from the looks of your error message, there might be more than just 10 live threads at any one time.
Thank you everyone for your help.
I did confirm my guess. The thread number is the number of threads that were created whether or not any of them were killed previously or not.
> Thank you everyone for your help.My pleasure.
>Suppose you had 6 threads called Thread-1, Thread-2, and so on up >to Thread-6 in the JVM that you designed. Then Thread-2 terminates. >What do you do, as a sensible JVM designer? Do you rename Thread->3, Thread-4, and so on? Or do you just leave them alone, suspecting >that people might get confused if you changed their names?
>A lot of questions can be answered by thinking about what somebody >with some common sense would do.
Just a curious question for you. Are you in theory filed or practice field?
> If you are creating 10 threads, killing those 10
> threads and creating 10 more, etc. 5,000 times, your
> GC might not have a chance to collect all the garbage
> and you could still run out of memory,
Are you talking about objects (e.g., threads) that are eligible for GC, but haven't actually been GCed yet, and therefore you could run out of memory?
No, that cannot happen. When you try to allocate memory, if there's not enough available, GC will run before OutOfMemoryError is thrown.
jverda at 2007-7-21 22:30:39 >

I am in a practiced field. A Software Engineer.
I believe that depends upon the JVM. I have seen some poorly written JVM's that have ran out of memory and shut down. I had to code around it.
> > If you are creating 10 threads, killing those 10
> > threads and creating 10 more, etc. 5,000 times,
> your
> > GC might not have a chance to collect all the
> garbage
> > and you could still run out of memory,
>
> Are you talking about objects (e.g., threads) that
> are eligible for GC, but haven't actually been GCed
> yet, and therefore you could run out of memory?
>
> No, that cannot happen. When you try to allocate
> memory, if there's not enough available, GC will run
> before OutOfMemoryError is thrown.
Message was edited by:
llemons
Oh Thanks llemons, but I meant to ask DrClap.
> I believe that depends upon the JVM. I have seen
> some poorly written JVM's that have ran out of memory
> and shut down. I had to code around it.
I don't believe that. You probably had a bug and altered the code so that you removed the bug or just made it harder to expose the bug.
Kaj
kajbja at 2007-7-21 22:30:39 >

I didn't actually change the code. I had to increase the memory heap large enough so that it wouldn't run out of memory before the GC was able to clear the memory. It still didn't solve all the problems, but unless the server was being hit extremely hard it, was alright after this.
> I didn't actually change the code. I had to increase
> the memory heap large enough so that it wouldn't run
> out of memory before the GC was able to clear the
> memory. It still didn't solve all the problems, but
> unless the server was being hit extremely hard it,
> was alright after this.
Then it wasn't a bug in the GC. The VM will do a full gc and throw an out of memory error if it still isn't able to find enough free memory to allocate/reserve for the object that you are trying to create.
kajbja at 2007-7-21 22:30:44 >

I've seen suns vm's core dump due to excessive thread creation. I think it is becuase, on many OS's there is a 1-1 relationship between java threads and OS threads/processes. OS level threads/processes are created with thier own stack (i.e. take up memory) that is not in the java heap. If your java heap is taking most of the available address space and then you create lots of threads you can blow the available address space and no longer create any new threads. This seems to cause the vm to exit unceremoniously.
The solution to the problem was to REDUCE the amount of memory that could be used as java heap. This gave enough free address space to handle thread creation.
matfud
The way I debugged it to find out what happened was to put a sleep and it never occured, but I didn't want the pauses so since the adding memory worked, they decided to go that route.This was NOT Sun's JVM, by the way. It was some JVM we were being forced to use.
I've seen suns vm's core dump due to excessive thread creation. I think it is becuase, on many OS's there is a 1-1 relationship between java >threads and OS threads/processes. OS level threads/processes are >created with thier own stack (i.e. take up memory) that is not in the >java heap. If your java heap is taking most of the available address >space and then you create lots of threads you can blow the available >address space and no longer create any new threads. This seems to >cause the vm to exit unceremoniously.
>The solution to the problem was to REDUCE the amount of memory >that could be used as java heap. This gave enough free address >space to handle thread creation.
>matfud
So, you mean I should reduce the -Xmx value? Do you think adding more JVM arguments would help? Like -Xms or -XX:MaxHeapFreeRatio or etc...
I don't believe any JVM arbuments will help until you find how/why so many threads are being created.
> I've seen suns vm's core dump due to excessive thread
> creation. I think it is becuase, on many OS's there
> is a 1-1 relationship between java >threads and OS
> threads/processes. OS level threads/processes are
> >created with thier own stack (i.e. take up memory)
> that is not in the >java heap. If your java heap is
> taking most of the available address >space and then
> you create lots of threads you can blow the available
> >address space and no longer create any new threads.
> This seems to >cause the vm to exit unceremoniously.
>
>
> >The solution to the problem was to REDUCE the amount
> of memory >that could be used as java heap. This gave
> enough free address >space to handle thread
> creation.
>
> >matfud
>
> So, you mean I should reduce the -Xmx value? Do you
> think adding more JVM arguments would help? Like -Xms
> or -XX:MaxHeapFreeRatio or etc...
No, not in your case. I was just commenting about an odd situation that can occur (and has for one of our customers).
Why on earth are you creating so many threads? Have you followed the earlier suggestion of finding all "new Thread" instances and putting debug logging (even System.err.out) before them? At least that way you will know how many threads you are creating.
It sounds like you may have, inside your code, a recursive call that ocasionally occurs which creates new threads. This could cause the OOME before a StackOverflow occurs.
matfud
> I don't believe any JVM arbuments will help until you
> find how/why so many threads are being created.
It's not important unless many of them still are alive.
@Op. Create a thread dump and see how many live threads you have.
You can also try to reduce the java thread stack size if you have lots of threads (-Xss)
Kaj
kajbja at 2007-7-21 22:30:44 >

I am not able to reproduce the bug until now. In normal cases using a profiler to check all kind of threads I have for starting up the GUI, loading a database, communicating with hardware and what not is up to 30 threads.
my suggestions are
1. print exit value of all loops
2. print messages on all object creation and destruction, specially your own classes
3. get your program verified by some other friend who have no prior knowledge of your code
From my experience i can tell that in your program some loop is creating classes and the loop is not exiting. Also the cause that it is happening sometimes and sometimes not is because the loop may not be triggering every time, that is it is triggered by some action handler.
regards
i_virus
Every time you create a thread or object, do a System.out.println(object); where object is the new object you created. Somewhere there is a loop that is creating a lot more threads than you think should be created. If it is in a GUI, then in the listener you probably are creating object(s) and only think that listener is only getting called once when in fact it may be being called hundreds or even thousands of times for each click or even mouse-over.
Without being able to see what you are doing with listeners it is almost impossible to tell.
> I am not able to reproduce the bug until now. In
> normal cases using a profiler to check all kind of
> threads I have for starting up the GUI, loading a
> database, communicating with hardware and what not is
> up to 30 threads.
You don't need a profiler to create a thread dump. You can create a thread dump from a running application if you are launching it from a console (windows) or if you are using unix/linux.
Kaj
kajbja at 2007-7-21 22:30:44 >
