Many times, we face issues of heap dump in our Java server. Heap increases because your system is generating many objects, and if their lifecycle is long and the system keeps on generating these objects, they will consume all the RAM and heap dump may occur. This may occur due to memory leaks, bad coding, etc.
But these are not the only reasons for heap dump. Once, I faced an issue with our customer where heap dump was occurring regularly, and after debugging, I found that the root cause of the issue was fine logging. You may be surprised how logging can cause heap dump.
Too much logging can cause heap dump even if they are fine level logs, not info logs. A surprising fact is that fine logging was not enabled.
Why it happened is as follows:
Generally, we log fine logging like in this example:
logger.fine("fine logs ") .
Now, what happens behind the scenes (in the JVM) is that even if fine logs are disabled except for the string “fine logs,” objects will be created as it is passed as an argument if the check for fine logging is inside the fine method.
If there are too many fine logs in the system and there are many threads available doing these operations with lots of fine logs, many char[] objects are created and fill the heap/RAM.
The Solution
There are two ways we should do fine logging.
1. Check the “if” condtion before every fine log, as follows:
if (logger.isFineLoggable()) {
logger.fine("your log ");
}
But, this does not look cool (lots of “if” in the code…)
2. With Java 8, Java gives a new API for logger, which does not take string as an argument; it takes function, which returns string as an argument and evaluates it when this function is called inside the fine method. This way, an object is not created until the function is called.
Here is an example:
logger.fine(() -> "fine logs"); .
With functional programming, the code looks nice and the problem was resolved.
Explanation of this blogs is as follows :
[…] Generally anytime when there are decisions taken in the code one can log on fine. The configurations read by the system we can log with fine, Some internal data structures created or any lifecycle event like DB connection closed we can log such things with fine. But do not add lots and lots of log , it can kill your system. […]
LikeLike
[…] See the original article […]
LikeLike
[…] Im Allgemeinen, wenn Sie Entscheidungen im Code treffen, unterschreiben Sie “fein. “Die vom System gelesenen Konfigurationen können auch mit eingeloggt werden”fein. ” Andere Beispiele für die Protokollierung “fein” enthält einige interne Datenstrukturen, die erstellt oder geändert wurden, alle Lebenszyklusereignisse wie eine geschlossene DB-Verbindung. Beachten Sie jedoch, dass das Hinzufügen von zu vielen Protokollen möglicherweise kill dein system. […]
LikeLike
[…] Generally anytime when there are decisions taken in the code, log “fine.” The configurations read by the system can also be logged with “fine.” Other examples of logging “fine” include some internal data structures created or modified, any lifecycle event like a closed DB connection. Just be aware that if you add too much log, it can kill your system. […]
LikeLike
[…] Generally anytime when there are decisions taken in the code, log “fine.” The configurations read by the system can also be logged with “fine.” Other examples of logging “fine” include some internal data structures created or modified, any lifecycle event like a closed DB connection. Just be aware that if you add too much log, it can kill your system. […]
LikeLike