How to check JVM memory usage: practical tips and methods to share
JVM (Java Virtual Machine) is the running environment of Java programs. It is responsible for converting Java bytecode into machine code, and manage the program's memory usage. Understanding JVM memory usage is very important to optimize program performance and solve memory leak problems. This article will introduce you to some practical tips and methods to view JVM memory usage, and provide specific code examples.
a. jmap: used to generate a memory snapshot of the Java heap. You can view the distribution of objects in the heap through the following command:
jmap -histo <pid>
Among them,
b. jstat: used to monitor the status and statistical information of the Java virtual machine. You can check the heap usage through the following command:
jstat -gc <pid>
where,
c. jconsole: Provides a graphical user interface to monitor the running status and performance of the Java virtual machine, and can view heap memory usage.
import javax.management.*; import java.lang.management.*; public class MemoryUsageDemo { public static void main(String[] args) throws Exception { // 获取MBean服务器 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // 声明ObjectName,用于获取MemoryMXBean ObjectName name = new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME); // 获取MemoryMXBean MemoryMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(mbs, name, MemoryMXBean.class); // 获取堆内存使用情况 MemoryUsage heapMemoryUsage = mxBean.getHeapMemoryUsage(); System.out.println("Heap Memory Usage:"); System.out.println("Initial: " + heapMemoryUsage.getInit() / (1024 * 1024) + "MB"); System.out.println("Used: " + heapMemoryUsage.getUsed() / (1024 * 1024) + "MB"); System.out.println("Committed: " + heapMemoryUsage.getCommitted() / (1024 * 1024) + "MB"); System.out.println("Max: " + heapMemoryUsage.getMax() / (1024 * 1024) + "MB"); // 获取非堆内存使用情况 MemoryUsage nonHeapMemoryUsage = mxBean.getNonHeapMemoryUsage(); System.out.println("Non-Heap Memory Usage:"); System.out.println("Initial: " + nonHeapMemoryUsage.getInit() / (1024 * 1024) + "MB"); System.out.println("Used: " + nonHeapMemoryUsage.getUsed() / (1024 * 1024) + "MB"); System.out.println("Committed: " + nonHeapMemoryUsage.getCommitted() / (1024 * 1024) + "MB"); System.out.println("Max: " + nonHeapMemoryUsage.getMax() / (1024 * 1024) + "MB"); } }
java -Xloggc:<logFilePath> -XX:+PrintGCDetails <ClassName>
Where, <logfilepath></logfilepath>
is the path of the log file, <classname></classname>
is the name of the Java class that needs to be run. After running the program, a file with the garbage collector log will be generated.
Summary:
By using command line tools, JMX and garbage collector logs, we can easily view the memory usage of the JVM. This is very helpful for optimizing program performance and solving memory leak problems. I hope that through the introduction of this article, you can master how to use these tools and techniques to check the memory usage of the JVM and improve the performance of Java programs.
The above is the detailed content of How to check JVM memory usage: practical tips and methods shared. For more information, please follow other related articles on the PHP Chinese website!