Memory Leaks in Java – Part 6

Memory leaks in Java - stylized image of a man at a huge dripping tap with a bucket to one side

This is part 6 of a multi-part series on memory leaks in Java. If you missed any of the last posts, you can find them here:

In the last few posts, we looked at some of the potential causes of memory leaks. We saw how the verbose output from the garbage collector could to help identify memory leaks.

In this post, we’ll take a quick look at profilers and two other tools, VisualVM and JConsole.

Profilers

Profilers are tools that can monitor memory usage and help detect memory leaks in an application. Profilers can help analyse how application resources are allocated, for example how much memory and CPU time is used by each method. This can identify and troubleshoot runtime issues.

Profilers can monitor Java bytecode operations at the JVM level. They can perform operations such as creating objects, executing loops, invoking methods, starting threads, and triggering garbage collections.

There are several tools that can be used to profile memory usage in Java applications. Some popular tools are:

  • Java VisualVM (free and open source).
  • JProfiler (a commercially licensed product; a free trial is available)
  • YourKit (also a commercially licensed product; a free fully functional trial is available).

IDEs such as Eclipse, IntelliJ IDEA and NetBeans also include profiling functionality.

VisualVM

Java VisualVM is a graphical tool for viewing detailed information about Java applications while running on a JVM.

It’s an all-in-one troubleshooting and profiling tool that can:

  • Display local and remote processes.
  • Display process configuration and environment settings.
  • Monitor process performance and memory usage.
  • Display all running threads.
  • Perform basic profiling.
  • Trigger and display thread and heap dumps.
  • Analyse core dumps.

Java VisualVM was included in the JDK up to Java 8. From Java 9 it is now a separate download from https://visualvm.github.io/.

Many of the separate JDK monitoring tools like jconsole, jstat, jinfo, jstack, and jmap are now part of Java VisualVM. These tools monitor different aspects of a running JVM instance. Some of the tools may not be available, depending on the installed version of Java.

Java VisualVM integrates these tools to collect a huge amount of JVM data. It then re-organizes and graphically displays this data. This allows us to view different aspects of multiple running Java applications in a uniform way. We can access and monitor both local and remote Java applications.

We can also extend the functionality of Java VisualVM by developing custom plug-ins.

JConsole

The JDK includes a number of separate troubleshooting tools. Depending on which version of Java we’re running, we might have access to command line tools like jconsole, jvisualvm, jmc, jstat and the like.

A powerful, yet easy to use tool is jconsole. This starts a graphical console which can monitor and manage Java applications and JVMs on local or remote computers. It is probably the first tool we should use to monitor an application. It has less functionality than VisualVM, but is useful enough for many tasks.

It has a number of tabbed screens. The overview screen shows heap memory usage, the number of live threads, the number of loaded classes, and CPU usage. There are extra screens for displaying more detailed information on memory usage, threads and loaded classes. We can view different areas of memory separately, for example heap and non-heap space and various memory pools. We can trigger garbage collections, and change time ranges. We can view and control MBeans in the application we are monitoring.

Collecting Profiling Data

The JDK provides a simple profiling tool called hprof for heap and CPU profiling. This can collect statistics on CPU usage and heap allocation. It can also report complete heap dumps, and the states of all the monitors and running threads.

hprof can be invoked by one of the following commands:

  • java -agentlib:hprof[=options] ClassToBeProfiled

  • java -Xrunhprof[:options] ClassToBeProfiled

We can run a JVM with the -Xprof option to print profiling data to standard output. This is supported up to Java 9.

See the HPROF: A Heap/CPU Profiling Tool page on the Oracle website for more information.

Also see the comprehensive page on using the HPROF Profiler on the IBM website.

Conclusion

In the next post, we’ll look at the Java Flight Recorder and Java Mission Control tools.

Please share your comments. I’d love to hear about your experiences with memory leaks and using these tools.

Stay safe and keep learning!

Leave a Comment

Your email address will not be published. Required fields are marked *

Code like a Java Guru!

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email info@incusdata.com

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.

Your Java tip is on its way!

Check that incusdata.com is an approved sender, so that your Java tips don’t land up in the spam folder.

Our privacy policy means your data is safe. You can unsubscribe from these tips at any time.