天天看点

jps,jstack,jheap,jhat(原创)

# 1、查看当前系统运行的 java 程序(简略)
jps -l

632 

6328 sun.tools.jps.Jps

# 2、查看当前系统运行的 java 程序(详细)
jps -lmvV

632  -Dosgi.requiredJavaVersion=1.8 [email protected]/eclipse-workspace -XX:+UseG1GC -XX:+UseStringDeduplication -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts -Dosgi.requiredJavaVersion=1.8 -Xms256m -Xmx1024m -Xdock:icon=../Resources/Eclipse.icns -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts -Declipse.p2.max.threads=10 -Doomph.update.url=http://download.eclipse.org/oomph/updates/milestone/latest -Doomph.redirection.index.redirection=index:/->http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/

6335 sun.tools.jps.Jps -lmvV -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home -Xms8m

# 3、查看java进程的线程执行状态

jstack -l 632

2018-05-24 17:20:58

Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode):

"Attach Listener" #24 daemon prio=9 os_prio=31 tid=0x00007f85590a9000 nid=0x12913 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"logback configurator timer" #19 daemon prio=5 os_prio=31 tid=0x00007f8557a0b000 nid=0x1070b in Object.wait() [0x0000700011e05000]

   java.lang.Thread.State: TIMED_WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

at java.util.TimerThread.mainLoop(Timer.java:552)

- locked <0x0000000786c51310> (a java.util.TaskQueue)

at java.util.TimerThread.run(Timer.java:505)

   Locked ownable synchronizers:

- None

"EMF Reference Cleaner" #18 daemon prio=5 os_prio=31 tid=0x00007f8556a3c800 nid=0x11a27 in Object.wait() [0x0000700011d02000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x0000000786b047e8> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)

- locked <0x0000000786b047e8> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)

at org.eclipse.emf.common.util.CommonUtil$1ReferenceClearingQueuePollingThread.run(CommonUtil.java:70)

   Locked ownable synchronizers:

- None

"Worker-JM" #17 prio=5 os_prio=31 tid=0x00007f855a214000 nid=0x11237 in Object.wait() [0x0000700011bff000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x0000000786904e30> (a java.util.ArrayList)

at org.eclipse.core.internal.jobs.InternalWorker.run(InternalWorker.java:59)

- locked <0x0000000786904e30> (a java.util.ArrayList)

   Locked ownable synchronizers:

- None

"SCR Component Actor" #16 daemon prio=5 os_prio=31 tid=0x00007f855a03f000 nid=0xcc07 in Object.wait() [0x0000700011afc000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x0000000786904fc0> (a java.util.LinkedList)

at java.lang.Object.wait(Object.java:502)

at org.apache.felix.scr.impl.ComponentActorThread.run(ComponentActorThread.java:80)

- locked <0x0000000786904fc0> (a java.util.LinkedList)

at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:

- None

"Framework Event Dispatcher: org.eclipse.osgi.internal.framework.EquinoxEventPublisher@41c89d2f" #15 daemon prio=5 os_prio=31 tid=0x00007f85579d7000 nid=0xca03 in Object.wait() [0x00007000119f9000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x0000000786904ff8> (a org.eclipse.osgi.framework.eventmgr.EventManager$EventThread)

at java.lang.Object.wait(Object.java:502)

at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.getNextEvent(EventManager.java:400)

- locked <0x0000000786904ff8> (a org.eclipse.osgi.framework.eventmgr.EventManager$EventThread)

at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:336)

   Locked ownable synchronizers:

- None

"Start Level: Equinox Container: 5d0c2c4a-f591-4e43-8c7e-392bc4862274" #14 daemon prio=5 os_prio=31 tid=0x00007f85590c1800 nid=0xc907 in Object.wait() [0x00007000118f6000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x0000000786b04818> (a org.eclipse.osgi.framework.eventmgr.EventManager$EventThread)

at java.lang.Object.wait(Object.java:502)

at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.getNextEvent(EventManager.java:400)

- locked <0x0000000786b04818> (a org.eclipse.osgi.framework.eventmgr.EventManager$EventThread)

at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:336)

   Locked ownable synchronizers:

- None

"Active Thread: Equinox Container: 5d0c2c4a-f591-4e43-8c7e-392bc4862274" #12 prio=5 os_prio=31 tid=0x00007f8557160800 nid=0xc717 waiting on condition [0x00007000117f3000]

   java.lang.Thread.State: TIMED_WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for  <0x0000000780f588a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)

at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)

at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)

at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)

at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)

at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:

- None

"Service Thread" #10 daemon prio=9 os_prio=31 tid=0x00007f85588fe000 nid=0x9303 runnable [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"C1 CompilerThread3" #9 daemon prio=9 os_prio=31 tid=0x00007f8556812000 nid=0x9103 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"C2 CompilerThread2" #8 daemon prio=9 os_prio=31 tid=0x00007f855a001800 nid=0x8f03 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"C2 CompilerThread1" #7 daemon prio=9 os_prio=31 tid=0x00007f85588fd800 nid=0x8d03 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 tid=0x00007f8557815000 nid=0x8b03 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"Signal Dispatcher" #5 daemon prio=9 os_prio=31 tid=0x00007f8556811000 nid=0x8903 runnable [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"Surrogate Locker Thread (Concurrent GC)" #4 daemon prio=9 os_prio=31 tid=0x00007f855a000800 nid=0x8603 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:

- None

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007f8556810800 nid=0x8203 in Object.wait() [0x0000700010bc6000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x0000000780a00c08> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)

- locked <0x0000000780a00c08> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)

at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

   Locked ownable synchronizers:

- None

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007f85588f5000 nid=0x8003 in Object.wait() [0x0000700010ac3000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

at java.lang.Object.wait(Object.java:502)

at java.lang.ref.Reference.tryHandlePending(Reference.java:191)

- locked <0x0000000780a00dc0> (a java.lang.ref.Reference$Lock)

at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

   Locked ownable synchronizers:

- None

"main" #1 prio=6 os_prio=31 tid=0x00007f855a808800 nid=0x307 runnable [0x00007fff54a1d000]

   java.lang.Thread.State: RUNNABLE

at org.eclipse.swt.internal.cocoa.OS.objc_msgSend_bool(Native Method)

at org.eclipse.swt.internal.cocoa.NSRunLoop.runMode(NSRunLoop.java:42)

at org.eclipse.swt.widgets.Display.sleep(Display.java:4830)

at org.eclipse.jface.window.Window.runEventLoop(Window.java:819)

at org.eclipse.jface.window.Window.open(Window.java:794)

at org.eclipse.ui.internal.ide.ChooseWorkspaceDialog.prompt(ChooseWorkspaceDialog.java:114)

at org.eclipse.ui.internal.ide.application.IDEApplication.promptForWorkspace(IDEApplication.java:364)

at org.eclipse.ui.internal.ide.application.IDEApplication.checkInstanceLocation(IDEApplication.java:278)

at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:141)

at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)

at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)

at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)

at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)

at org.eclipse.equinox.launcher.Main.run(Main.java:1499)

   Locked ownable synchronizers:

- None

"VM Thread" os_prio=31 tid=0x00007f85588f0800 nid=0x7e03 runnable 

"Gang worker#0 (Parallel GC Threads)" os_prio=31 tid=0x00007f8558810800 nid=0x5403 runnable 

"Gang worker#1 (Parallel GC Threads)" os_prio=31 tid=0x00007f8558814000 nid=0x5603 runnable 

"Gang worker#2 (Parallel GC Threads)" os_prio=31 tid=0x00007f855881a800 nid=0x5803 runnable 

"Gang worker#3 (Parallel GC Threads)" os_prio=31 tid=0x00007f855881b000 nid=0x5a03 runnable 

"Gang worker#4 (Parallel GC Threads)" os_prio=31 tid=0x00007f855881b800 nid=0x5c03 runnable 

"Gang worker#5 (Parallel GC Threads)" os_prio=31 tid=0x00007f855881c000 nid=0x5e03 runnable 

"Gang worker#6 (Parallel GC Threads)" os_prio=31 tid=0x00007f855881d000 nid=0x6003 runnable 

"Gang worker#7 (Parallel GC Threads)" os_prio=31 tid=0x00007f855881d800 nid=0x6203 runnable 

"G1 Main Concurrent Mark GC Thread" os_prio=31 tid=0x00007f8558833800 nid=0x7603 runnable 

"Gang worker#0 (G1 Parallel Marking Threads)" os_prio=31 tid=0x00007f8559003000 nid=0x7803 runnable 

"Gang worker#1 (G1 Parallel Marking Threads)" os_prio=31 tid=0x00007f855a006800 nid=0x7a03 runnable 

"G1 Concurrent Refinement Thread#0" os_prio=31 tid=0x00007f8558824000 nid=0x7403 runnable 

"G1 Concurrent Refinement Thread#1" os_prio=31 tid=0x00007f8558823800 nid=0x7203 runnable 

"G1 Concurrent Refinement Thread#2" os_prio=31 tid=0x00007f8558822800 nid=0x7003 runnable 

"G1 Concurrent Refinement Thread#3" os_prio=31 tid=0x00007f8558822000 nid=0x6e03 runnable 

"G1 Concurrent Refinement Thread#4" os_prio=31 tid=0x00007f8558821000 nid=0x6c03 runnable 

"G1 Concurrent Refinement Thread#5" os_prio=31 tid=0x00007f8558820800 nid=0x6a03 runnable 

"G1 Concurrent Refinement Thread#6" os_prio=31 tid=0x00007f855881f800 nid=0x6803 runnable 

"G1 Concurrent Refinement Thread#7" os_prio=31 tid=0x00007f855881f000 nid=0x6603 runnable 

"G1 Concurrent Refinement Thread#8" os_prio=31 tid=0x00007f855881e000 nid=0x6403 runnable 

"String Deduplication Thread" os_prio=31 tid=0x00007f85588d7800 nid=0x7c03 runnable 

"VM Periodic Task Thread" os_prio=31 tid=0x00007f855900b800 nid=0x9503 waiting on condition 

JNI global references: 392

# 4、查看分类型的堆统计

jmap -heap 632

Attaching to process ID 632, please wait...

Debugger attached successfully.

Server compiler detected.

JVM version is 25.131-b11

using thread-local object allocation.

Garbage-First (G1) GC with 8 thread(s)

Heap Configuration:

   MinHeapFreeRatio         = 40

   MaxHeapFreeRatio         = 70

   MaxHeapSize              = 1073741824 (1024.0MB)

   NewSize                  = 1363144 (1.2999954223632812MB)

   MaxNewSize               = 643825664 (614.0MB)

   OldSize                  = 5452592 (5.1999969482421875MB)

   NewRatio                 = 2

   SurvivorRatio            = 8

   MetaspaceSize            = 21807104 (20.796875MB)

   CompressedClassSpaceSize = 1073741824 (1024.0MB)

   MaxMetaspaceSize         = 17592186044415 MB

   G1HeapRegionSize         = 1048576 (1.0MB)

Heap Usage:

G1 Heap:

   regions  = 1024

   capacity = 1073741824 (1024.0MB)

   used     = 123350528 (117.63623046875MB)

   free     = 950391296 (906.36376953125MB)

   11.487913131713867% used

G1 Young Generation:

Eden Space:

   regions  = 91

   capacity = 162529280 (155.0MB)

   used     = 95420416 (91.0MB)

   free     = 67108864 (64.0MB)

   58.70967741935484% used

Survivor Space:

   regions  = 6

   capacity = 6291456 (6.0MB)

   used     = 6291456 (6.0MB)

   free     = 0 (0.0MB)

   100.0% used

G1 Old Generation:

   regions  = 21

   capacity = 99614720 (95.0MB)

   used     = 21638656 (20.63623046875MB)

   free     = 77976064 (74.36376953125MB)

   21.722347861842106% used

10800 interned Strings occupying 915944 bytes.

# 5、查看基于 classloader 的堆统计

jmap -clstats 632

Attaching to process ID 632, please wait...

Debugger attached successfully.

Server compiler detected.

JVM version is 25.131-b11

finding class loader instances ..done.

computing per loader stat ..done.

please wait.. computing liveness.......liveness analysis may be inaccurate ...

class_loader classes bytes parent_loader alive? type

<bootstrap> 1413 2614211   null  live <internal>

0x0000000786c19940 73 139502 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786920a10 0 0 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c793c0 69 143761 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000781400048 69 176221 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c18dc8 19 38354 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c191c8 53 109827 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c1c248 87 204940 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786b4a438 0 0 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c055d0 11 7916 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c18550 3 2593 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c1a2d0 65 118499 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786b49da0 0 0 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c063d8 184 415436 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c2c0d8 7 18717 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c79358 16 38634 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c05a60 11 12415 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c2d6e0 223 452432 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c00168 97 410884 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c05168 248 826728 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c15968 33 79295 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c15e68 60 180492 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

0x0000000786c21be8 97 212559 0x0000000780a04ed8 live org/eclipse/osgi/internal/loader/[email protected]

....

total = 73 5103 11314038     N/A    alive=70, dead=3     N/A  

# 6、导出堆的使用状态到一个文件

jmap -dump:live,format=b,file=heap.bin 632

jhat heap.bin 

# 分析上面一步导出的堆统计文件

jhat heap.bin 

Reading from heap.bin...

Dump file created Thu May 24 17:41:00 CST 2018

Snapshot read, resolving...

Resolving 269783 objects...

Chasing references, expect 53 dots.....................................................

Eliminating duplicate references.....................................................

Snapshot resolved.

Started HTTP server on port 7000

Server is ready.

# 7、在浏览器中查看堆信息

http://127.0.0.1:7000

信息的最后部分,有几个链接,可以点击,从而看到按照不同纬度进行组合、排序的统计。在查看问题的时候,应该很有用。不明白为什么不把这几个链接放在最顶部。

Other Queries

  • All classes including platform
  • Show all members of the rootset
  • Show instance counts for all classes (including platform)
  • Show instance counts for all classes (excluding platform)
  • Show heap histogram
  • Show finalizer summary
  • Execute Object Query Language (OQL) query
例如,点击 Show instance counts for all classes (including platform),能看到各个Class的实例个数,已经按多少降序排好,如:
  • 50719 instances of class java.util.HashMap$Node 

    29557 instances of class java.lang.String 

    23679 instances of class [C 

    14285 instances of class java.util.HashMap 

    13740 instances of class [Ljava.util.HashMap$Node; 

    8192 instances of class [Ljava.lang.Object; 

    7125 instances of class java.util.HashMap$EntrySet 

    6766 instances of class java.util.HashMap$KeySet 

    6395 instances of class java.util.Collections$UnmodifiableMap 

    6240 instances of class java.util.Collections$UnmodifiableSet 

    6231 instances of class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet 

    6076 instances of class java.util.ArrayList 

    5783 instances of class org.eclipse.osgi.container.ModuleCapability 

    5645 instances of class org.eclipse.osgi.container.ModuleRequirement 

    5524 instances of class java.util.HashSet 

    5194 instances of class java.util.Collections$SingletonSet 

    5142 instances of class org.eclipse.osgi.container.ModuleWire 

    3458 instances of class java.lang.Class 

    3236 instances of class java.lang.Object 

    2598 instances of class java.util.Collections$SingletonMap 

    2597 instances of class java.util.AbstractMap$SimpleImmutableEntry 

    2222 instances of class java.util.Hashtable$Entry 

【完】