DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Rust-Native Alternatives to Spark SQL and DataFrame Workloads
  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes
  • Memory Optimization and Utilization in Java 25 LTS: Practical Best Practices
  • Shrink a Bloated Git Repository and Optimize Pack Files

Trending

  • Native SQL in Java Without JDBC Boilerplate — Meet Ujorm3
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • AI Paradigm Shift: Analytics Without SQL
  • Feature Flag Debt: Performance Impact in Enterprise Applications
  1. DZone
  2. Coding
  3. Languages
  4. The Solution — Too Much Fine Logging Causing Heap Dump

The Solution — Too Much Fine Logging Causing Heap Dump

Too much fine logging can still cause heap dump in your Java server. Learn how to prevent this problem.

By 
Sahil Aggarwal user avatar
Sahil Aggarwal
·
Nov. 07, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
8.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.

garbage collection Dump (program)

Opinions expressed by DZone contributors are their own.

Related

  • Rust-Native Alternatives to Spark SQL and DataFrame Workloads
  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes
  • Memory Optimization and Utilization in Java 25 LTS: Practical Best Practices
  • Shrink a Bloated Git Repository and Optimize Pack Files

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook