Identifying large objects consuming memory in the Java heap

Recently, we experienced some excessive memory consumption that was leading our servers into an out-of-memory (OOM) condition. To troubleshoot the issue, we needed a way to identify any large objects that were filling up the heap. As it turns out, there’s a JVM argument for this.

Generic JVM argument: -Xdump:stack:events=allocation,filter

If you set this option, whenever an allocation request is made for an object greater than or equal to the given size, the Java stack trace corresponding to the thread requesting the allocation is printed into the standard error log. From this stack trace, you can easily see which actual Java class is eating up all your memory. For example, the following setting will print the stack information for all allocations over 10 megabytes to the native_stderr.log:

-Xdump:stack:events=allocation,filter=#10m

You can also view an allocation within a certain range of values. The following, for example, will print the stack information for all allocations between 2m and 4m to the native_stderr.log

-Xdump:stack:events=allocation,filter=#2m..4m

You can read more about this in the following IBM technote:

Identifying the Java stack of a thread making an allocation request larger than a certain size