0

How to get current memory in use, no. of threads, CPU usage, load metrics etc. of a Java application that runs on Tomcat or Jetty. I need these metrics for finding the current state of the application if it's overloaded or underutilized etc. and I want to get this info from the application itself. Please guide.

JSC
  • 9
  • 3

1 Answers1

2

top

Unix-oriented operating systems (macOS, BSD, Linux, etc.) provide the command-line tool top to view processes currently running. You can view your Java process’ use of memory and more.

In macOS, the Apple app Activity Monitor provides this functionality in a GUI. See User Guide. Other platforms likely have a similar tool.

DTrace

DTrace is a technology for comprehensive dynamic tracing of both OS (kernel) and apps at runtime, with surprisingly little impact on performance.

Various operating systems incorporate this technology. In macOS, Apple wraps DTrace as their Instruments tooling. Some flavors of BSD also have DTrace built-in.

I believe DTrace can be used to monitor a running JVM, but I do not recall the details.

Debugger

Some IDEs are able to attach a debugger to your web app running in Tomcat or Jetty. Useful for development and testing, but not deployment.

Flight Recorder & Mission Control

logo for Mission Control

Flight Recorder is a tool for profiling and event collection within a running JVM. See JEP 328: Flight Recorder.

Mission Control is a set of tools for detailed analysis of the extensive data collected by Flight Recorder. See wiki page. See this page by Oracle, but I’ve not checked to verify it is up-to-date.

Formerly commercial products from Oracle, both Flight Recorder and Mission Control are now housed at the OpenJDK project as open-source and free-of-cost projects.

JMX

You said:

I want to get this info from the application itself.

Then JMX is for you.

Java Management Extensions (JMX) is a framework to export current runtime conditions information from within your app to external tools in a standards-based approach.

You can use your choice of a variety of monitoring tools to view the runtime info. This includes tools built on the Simple Network Management Protocol (SNMP) standard.

You can write your own JMX-compliant Managed Bean (MBeans) classes to report your own information. See tutorial by Oracle.

➥ Tomcat comes bundled with its own MBeans. See documentation, Monitoring and Managing Tomcat.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
  • Thanks, Basil, I will try the Flight recorder. – JSC Apr 02 '21 at 04:50
  • `top` is a pretty bad tool to evaluate Java programs since it gives you the values for the jvm, not the program. – daniu Apr 11 '21 at 21:33
  • @daniu In real-world practice, how often do people have multiple unrelated apps running within a single JVM? – Basil Bourque Apr 11 '21 at 21:36
  • The issue is not so much that there might be different apps running inside the same vm, but that the data doesn't show the actual app's behavior. You can easily have the VM grab the 700MB -mx space while your app never uses more than 50. Or the app's memory usage continually climbs due to a memory leak, but the VM stays at its 700MB until the app can't get more and crashes. That's all memory obviously, CPU usage should be somewhat more useful. – daniu Apr 12 '21 at 10:20