Reputation: 8705
I have a Java/Java EE Web Application.
Often when I see that the application stops responding because of high heap usage (or out of memory scenario), I also see that threads are blocked (via a thread dump) - often on logging, and also on random things.
I have seen this happen more than once in the web application.
Is there any correlation between an out of memory scenario and blocked threads?
Upvotes: 2
Views: 1901
Reputation: 10028
Yes, because threads are where your code executes and your code needs memory. Java is object-oriented, so creation of new objects is an extremely common occurrence. When a JVM is having memory issues, attempts to allocate more memory block until memory can be granted.
Interfacing with external systems (I/O) is a common point to see threads blocking because these often involve good size chunks of memory allocation (such as string buffers for formatting, reading in a .class file by a class loader, generating objects for a database result set).
This is one of many reasons why troubleshooting OutOfMemoryError can be very difficult. When your heap space is running low / exhausted, every thing slows down and breaks to point where separating the symptoms from the cause becomes difficult.
Upvotes: 2
Reputation: 68715
Yes there is a correlation. Although threads share the heap but they have their own stack. Both are the memory allocated from the available memory. A thread may be doing some work as you have mentioned in your case logging. For logging thread may be holding some logs in memory and will be trying to put them in the log file. As different logging threads are there, so they will be waiting for turns to get the access to the log file. If threads are waiting too long for the file then they will be holding the log data in memory for long. If this keep happening there will too many threads with too many data in memory. Eventually JVM will encounter out of memory when someone tries to get memory and there is nothing available.
Upvotes: 0
Reputation: 454
Yes there is a direct correlation between OOM and blocked threads. This is due to the reason that the thread is trying to allocate memory on the heap and not able get adequate memory. Mostly you will see blocked threads around logging, class loading, resource lookup, IO. These all are the cases where new memory allocation is required.
Upvotes: 2