jvm 运行时数据区
jvm 运行时数据区包括且不限于以下几个部分:
- 堆(heap): 用于存储对象实例和数组。堆内存的分配和释放由垃圾回收器进行管理。
- 方法区(method area): 用于存储类的信息、静态变量、常量等。jdk 8 后方法区位于 metaspace。
- 虚拟机栈(vm stack): 用于存储方法的局部变量、参数值等。
- 本地方法栈(native method stack): 用于存储本地方法调用的相关信息。
- 程序计数器(program counter): 用于记录当前线程执行的字节码指令地址。
观察 jvm 运行时数据区内存使用的命令
观察 jvm 运行时数据区内存情况可以使用 jcmd 命令:
# 需要 JVM 参数配置 -XX:NativeMemoryTracking=summary 或 -XX:NativeMemoryTracking=detail
# 使用前需要先 su 切换到 pid 的用户(top 中可看,一般 java 进程的用户是 tomcat)# 应用启动后输入
jcmd pid VM.native_memory summary
或
jcmd pid VM.native_memory detailjcmd 300 VM.native_memory summary
输出内容:
输出的 VM.native_memory 全部内容
[tomcat@preparedocker-dep2-7f64d558f8-gfkmw default.qunar.com]$ jcmd 385 VM.native_memory summary
385:Native Memory Tracking:(Omitting categories weighting less than 1KB)Total: reserved=14696320KB, committed=13246344KB
- Java Heap (reserved=11534336KB, committed=11534336KB)(mmap: reserved=11534336KB, committed=11534336KB)- Class (reserved=1055872KB, committed=34688KB)(classes #41524)( instance classes #39223, array classes #2301)(malloc=7296KB #139986)(mmap: reserved=1048576KB, committed=27392KB)( Metadata: )( reserved=196608KB, committed=188544KB)( used=186864KB)( waste=1680KB =0.89%)( Class space:)( reserved=1048576KB, committed=27392KB)( used=25440KB)( waste=1952KB =7.13%)- Thread (reserved=574463KB, committed=245719KB)(thread #2115)(stack: reserved=568244KB, committed=239500KB)(malloc=3741KB #12726)(arena=2478KB #4229)- Code (reserved=262510KB, committed=170658KB)(malloc=14822KB #46616)(mmap: reserved=247688KB, committed=155836KB)- GC (reserved=506924KB, committed=506924KB)(malloc=45760KB #83524)(mmap: reserved=461164KB, committed=461164KB)- Compiler (reserved=16021KB, committed=16021KB)(malloc=15856KB #9536)(arena=165KB #5)- Internal (reserved=95580KB, committed=95576KB)(malloc=95540KB #150203)(mmap: reserved=40KB, committed=36KB)- Other (reserved=353690KB, committed=353690KB)(malloc=353690KB #1001)- Symbol (reserved=38658KB, committed=38658KB)(malloc=36572KB #993784)(arena=2086KB #1)- Native Memory Tracking (reserved=24133KB, committed=24133KB)(malloc=302KB #4295)(tracking overhead=23831KB)- Shared class space (reserved=12288KB, committed=12160KB)(mmap: reserved=12288KB, committed=12160KB)- Arena Chunk (reserved=12414KB, committed=12414KB)(malloc=12414KB)- Tracing (reserved=32KB, committed=32KB)(arena=32KB #1)- Logging (reserved=7KB, committed=7KB)(malloc=7KB #288)- Arguments (reserved=4KB, committed=4KB)(malloc=4KB #138)- Module (reserved=4051KB, committed=4051KB)(malloc=4051KB #14643)- Safepoint (reserved=8KB, committed=8KB)(mmap: reserved=8KB, committed=8KB)- Synchronization (reserved=872KB, committed=872KB)(malloc=872KB #7198)- Serviceability (reserved=2042KB, committed=2042KB)(malloc=2042KB #35392)- Metaspace (reserved=198597KB, committed=190533KB)(malloc=1989KB #2357)(mmap: reserved=196608KB, committed=188544KB)- String Deduplication (reserved=1KB, committed=1KB)(malloc=1KB #8)- Object Monitors (reserved=3817KB, committed=3817KB)(malloc=3817KB #18790)
命令权限问题
如执行 jcmd pid VM.native_memory summary 命令后无输出,需要通过 su 切换到 pid 的用户(top 中可看,一般 java 进程的用户是 tomcat)