How to check CPU and memory usage in Java?

I need to check CPU and memory usage for server in java, does anyone know how to do this?

+93
java cpu ram operating-system
Sep 16 '08 at 17:13
source share
15 answers

If you are looking specifically for memory in the JVM:

Runtime runtime = Runtime.getRuntime(); NumberFormat format = NumberFormat.getInstance(); StringBuilder sb = new StringBuilder(); long maxMemory = runtime.maxMemory(); long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); sb.append("free memory: " + format.format(freeMemory / 1024) + "<br/>"); sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "<br/>"); sb.append("max memory: " + format.format(maxMemory / 1024) + "<br/>"); sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "<br/>"); 

However, they should only be taken as an assessment ...

+70
Sep 16 '08 at 17:22
source share
 package mkd.Utils; import java.io.File; import java.text.NumberFormat; public class systemInfo { private Runtime runtime = Runtime.getRuntime(); public String Info() { StringBuilder sb = new StringBuilder(); sb.append(this.OsInfo()); sb.append(this.MemInfo()); sb.append(this.DiskInfo()); return sb.toString(); } public String OSname() { return System.getProperty("os.name"); } public String OSversion() { return System.getProperty("os.version"); } public String OsArch() { return System.getProperty("os.arch"); } public long totalMem() { return Runtime.getRuntime().totalMemory(); } public long usedMem() { return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); } public String MemInfo() { NumberFormat format = NumberFormat.getInstance(); StringBuilder sb = new StringBuilder(); long maxMemory = runtime.maxMemory(); long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); sb.append("Free memory: "); sb.append(format.format(freeMemory / 1024)); sb.append("<br/>"); sb.append("Allocated memory: "); sb.append(format.format(allocatedMemory / 1024)); sb.append("<br/>"); sb.append("Max memory: "); sb.append(format.format(maxMemory / 1024)); sb.append("<br/>"); sb.append("Total free memory: "); sb.append(format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024)); sb.append("<br/>"); return sb.toString(); } public String OsInfo() { StringBuilder sb = new StringBuilder(); sb.append("OS: "); sb.append(this.OSname()); sb.append("<br/>"); sb.append("Version: "); sb.append(this.OSversion()); sb.append("<br/>"); sb.append(": "); sb.append(this.OsArch()); sb.append("<br/>"); sb.append("Available processors (cores): "); sb.append(runtime.availableProcessors()); sb.append("<br/>"); return sb.toString(); } public String DiskInfo() { /* Get a list of all filesystem roots on this system */ File[] roots = File.listRoots(); StringBuilder sb = new StringBuilder(); /* For each filesystem root, print some info */ for (File root : roots) { sb.append("File system root: "); sb.append(root.getAbsolutePath()); sb.append("<br/>"); sb.append("Total space (bytes): "); sb.append(root.getTotalSpace()); sb.append("<br/>"); sb.append("Free space (bytes): "); sb.append(root.getFreeSpace()); sb.append("<br/>"); sb.append("Usable space (bytes): "); sb.append(root.getUsableSpace()); sb.append("<br/>"); } return sb.toString(); } } 
+19
Jan 23 2018-12-23T00:
source share

If you use the Sun JVM and are interested in using the internal memory of the application (how much of the allocated memory is used by your application), I prefer to include the JVM's built-in garbage collection log. You simply add -verbose: gc to the run command.

From the Sun documentation:

-Verbose: gc command line argument prints information on each collection. Note that the output format is -verbose: gc to change between versions of the J2SE platform. For example, here is the output from a large server application:

 [GC 325407K->83000K(776768K), 0.2300771 secs] [GC 325816K->83372K(776768K), 0.2454258 secs] [Full GC 267628K->83769K(776768K), 1.8479984 secs] 

Here we see two small collections and one important. Figures before and after the arrow

 325407K->83000K (in the first line) 

indicate the total size of live objects before and after the collection debris, respectively. After small collections, the account includes objects that are not necessarily alive, but cannot be restored, either because they are directly alive or because they are inside or referenced from the created generation. Number in parentheses

 (776768K) (in the first line) 

- the total available space, not counting the space in the permanent generation, which is a common bunch minus one of the survivors. A small collection took about a quarter of a second.

 0.2300771 secs (in the first line) 

For more details see: http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

+17
Sep 16 '08 at 19:39
source share

From here

  OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); int availableProcessors = operatingSystemMXBean.getAvailableProcessors(); long prevUpTime = runtimeMXBean.getUptime(); long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime(); double cpuUsage; try { Thread.sleep(500); } catch (Exception ignored) { } operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); long upTime = runtimeMXBean.getUptime(); long processCpuTime = operatingSystemMXBean.getProcessCpuTime(); long elapsedCpu = processCpuTime - prevProcessCpuTime; long elapsedTime = upTime - prevUpTime; cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors)); System.out.println("Java CPU: " + cpuUsage); 
+15
Mar 31 '13 at 18:56
source share

JMX provided by MXBeans (ThreadMXBean, etc.) will provide you with memory and processor utilization.

 OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); operatingSystemMXBean.getSystemCpuLoad(); 
+9
Sep 16 '08 at 18:00
source share

To use memory, the following will work:

 long total = Runtime.getRuntime().totalMemory(); long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 

To use the CPU, you need to use an external application to measure it.

+8
Sep 16 '08 at 17:20
source share

Starting with Java 1.5, the JDK comes with a new tool: JConsole , which can show you processor and memory usage for any 1.5 or later JVM. He can make diagrams of these parameters, export to CSV, show the number of loaded classes, the number of instances, deadlocks, threads, etc.

+5
Sep 16 '08 at 17:27
source share

If you are using the runtime / totalMemory solution that has been posted in many answers here (I have done this a lot), first force two garbage collections if you want to get sufficiently accurate / consistent results.

For effiency, Java usually allows garbage to fill all the memory before forcing the GC, and even then it is usually not a full GC, so your results for runtime.freeMemory () are always somewhere between the "real" amount of free memory and 0.

The first GC doesn't get everything, it gets the most.

The climb is that if you just make a call to freeMemory (), you will get a number that is completely useless and varies a lot, but if it does 2 gc, then this is a very reliable sensor. It also makes the MUCH procedure slower (maybe seconds).

+4
Sep 16 '08 at 17:22
source share

A Java Runtime object can report JVM memory usage. To consume the CPU, you will need to use an external utility such as Unix top or Windows Process Manager.

+3
Sep 16 '08 at 17:19
source share

Here is a simple code to calculate the current memory usage in megabytes:

 double currentMemory = ( (double)((double)(Runtime.getRuntime().totalMemory()/1024)/1024))- ((double)((double)(Runtime.getRuntime().freeMemory()/1024)/1024)); 
+2
Oct 24 '11 at 2:13
source share

I would also add the following method to track CPU usage:

 import java.lang.management.ManagementFactory; import com.sun.management.OperatingSystemMXBean; double getCpuLoad() { OperatingSystemMXBean osBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory. getPlatformMXBeans(OperatingSystemMXBean.class); return osBean.getProcessCpuLoad(); } 

You can read here

+2
Jul 02 '15 at
source share

If you use Tomcat, check out the Psi Probe , which allows you to track the consumption of internal and external memory, as well as many other areas.

+1
Sep 16 '08 at 17:18
source share

JConsole is an easy way to track a running Java application, or you can use Profiler to get more details about your application. I like to use NetBeans Profiler for this.

+1
Sep 16 '08 at 17:21
source share

YourKit Java Profiler is a great commercial solution. You can find more information in the documents CPU profiling and memory profiling .

+1
Sep 16 '08 at 17:52
source share

For Eclipse, you can use TPTP (Test and Performance Tools Platform) to analyze memory usage, etc. Additional Information

0
Nov 07 '11 at 15:55
source share



All Articles