Java:如何查看java应用加载了哪些类?

先直接给出结论:

#1

jcmd GC.class_histogram

或 #2

jmap -histo

 或 #3

jmap -histo:live

执行结果如下所示:

 num     #instances         #bytes  class name (module)
-------------------------------------------------------
   1:        476831       43623368  [B ([email protected])
   2:        176012       21862544  [C ([email protected])
   3:        571102       18275264  java.util.HashMap$Node ([email protected])
   4:        666997       16007928  java.lang.String ([email protected])
   5:        151332       11937928  [I ([email protected])
   6:        117792        9024648  [Ljava.util.HashMap$Node; ([email protected])
   7:        140910        8463264  [Ljava.lang.Object; ([email protected])
   8:         68296        6286424  [J ([email protected])
   9:         92323        4431504  java.util.HashMap ([email protected])
  10:         87662        3506480  java.util.LinkedHashMap$Entry ([email protected])
  11:         25716        3158832  java.lang.Class ([email protected])
  12:         58626        2739904  [Ljava.lang.String; ([email protected])
  13:        104695        2512680  java.util.ArrayList ([email protected])
  14:         25001        2400096  org.apache.maven.model.Dependency
  15:         66238        2119616  java.util.Collections$UnmodifiableMap ([email protected])
  16:         35971        2014376  java.util.LinkedHashMap ([email protected])
  17:         27768        1999296  org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImpl
  18:         53012        1696384  org.apache.maven.model.InputLocation
  19:         16823        1586224  [[C ([email protected])
  20:         34742        1389680  org.eclipse.aether.artifact.DefaultArtifact
  21:         42643        1364576  java.util.concurrent.ConcurrentHashMap$Node ([email protected])
  22:         48042        1153008  org.eclipse.emf.ecore.util.EcoreUtil$ProperContentIterator
  23:         34663        1109216  org.eclipse.aether.graph.Dependency
  24:         33835        1082720  jdk.internal.jimage.ImageReader$Resource
  25:         43937        1054488  java.lang.StringBuilder ([email protected])
  26:         25816        1032640  java.util.WeakHashMap$Entry ([email protected])
  27:         29809         953888  jdk.internal.jimage.ImageReader$Resource
  28:         39024         936576  org.eclipse.emf.ecore.impl.BasicEObjectImpl$3
  29:         10551         928488  java.lang.reflect.Method ([email protected])
  30:         33835         812040  jdk.internal.jimage.ImageLocation
  31:         29809         715416  jdk.internal.jimage.ImageLocation
  32:          1597         675072  [Ljava.util.concurrent.ConcurrentHashMap$Node; ([email protected])
  33:         16695         667800  org.eclipse.emf.ecore.util.EcoreEMap$DelegateEObjectContainmentEList
  34:         27768         666432  org.eclipse.emf.ecore.util.EContentsEList
  35:         19935         637920  org.eclipse.emf.common.util.AbstractEList$EIterator
  36:         15468         618720  org.eclipse.emf.common.util.AbstractEList$NonResolvingEListIterator
  37:         15359         614360  org.eclipse.emf.ecore.util.EcoreEMap
  38:         17879         572128  java.util.Hashtable$Entry ([email protected])
  39:         16014         512448  org.eclipse.emf.common.util.URI$LazyFragmentInitializer
  40:         15363         491616  org.eclipse.emf.common.util.BasicEMap$View
  41:         11795         471800  org.codehaus.plexus.util.xml.Xpp3Dom
  42:         11673         466920  org.eclipse.equinox.internal.p2.metadata.RequiredCapability
  43:            13         426192  [Ljava.util.concurrent.ForkJoinTask; ([email protected])
  44:          1160         417440  [[Ljava.lang.Object; ([email protected])
  45:         17894         405280  [Ljava.lang.Class; ([email protected])
  46:         24815         397040  org.eclipse.jdt.internal.ui.text.spelling.engine.AbstractSpellDictionary$ByteArrayWrapper
  47:         16022         384528  org.eclipse.emf.common.util.URI$Fragment
  48:         16014         384336  org.eclipse.emf.ecore.impl.MinimalEObjectImpl$1ArrayDelegatingAdapterList
  49:         11738         375616  org.eclipse.osgi.container.ModuleCapability
  50:          4177         375112  [S ([email protected])

它会根据占用内存的大小进行排序。

其中#2和#3的区别是,#3会先做一次FullGC,然后把存活下来的对象打印出来

如果还需要知道是否加载了某些类,可以再加上grep,比如想知道是否用到了log4j

jmap -histo | grep log4j

其实jcmd和jmap还有很多参数,可以用来获取java应用的信息,这些参数基本可以做到望文生义:

C:\Java\jdk1.8.0_261\bin>jcmd help
Error parsing arguments: No command specified

Usage: jcmd
   or: jcmd -l
   or: jcmd -h

  command must be a valid jcmd command for the selected jvm.
  Use the command "help" to see which commands are available.
  If the pid is 0, commands will be sent to all Java processes.
  The main class argument will be used to match (either partially
  or fully) the class used to start Java.
  If no options are given, lists Java processes (same as -p).

  PerfCounter.print display the counters exposed by this process
  -f  read and execute commands from the file
  -l  list JVM processes on the local machine
  -h  this help

 7140是一个java应用的进程编号

C:\Java\jdk1.8.0_261\bin>jcmd 7140 help
7140:
The following commands are available:
Compiler.CodeHeap_Analytics
Compiler.codecache
Compiler.codelist
Compiler.directives_add
Compiler.directives_clear
Compiler.directives_print
Compiler.directives_remove
Compiler.queue
GC.class_histogram
GC.finalizer_info
GC.heap_dump
GC.heap_info
GC.run
GC.run_finalization
JFR.check
JFR.configure
JFR.dump
JFR.start
JFR.stop
JVMTI.agent_load
JVMTI.data_dump
ManagementAgent.start
ManagementAgent.start_local
ManagementAgent.status
ManagementAgent.stop
Thread.print
VM.class_hierarchy
VM.classloader_stats
VM.classloaders
VM.command_line
VM.dynlibs
VM.events
VM.flags
VM.info
VM.log
VM.metaspace
VM.native_memory
VM.print_touched_methods
VM.set_flag
VM.stringtable
VM.symboltable
VM.system_properties
VM.systemdictionary
VM.uptime
VM.version
help

For more information about a specific command use 'help '.

C:\Java\jdk1.8.0_261\bin>jmap -h
Usage:
    jmap [option]
        (to connect to running process)
    jmap [option]
        (to connect to a core file)
    jmap [option] [server_id@]
        (to connect to remote debug server)

where

 虽然知道了JVM中加载的哪些类,怎么知道这些类属于什么jar呢?

java -verbose:class

从JDK9开始,可以使用如下参数:

java -Xlog:class+load=info:classloaded.txt

 这些参数都需要在进程启动中添加。

参考文档

Java Platform, Standard Edition Troubleshooting Guide
https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/toc.html

The jmap Utility
https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr014.html#class_loader_statistics

The jcmd Utility
https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html#BABEHABG

你可能感兴趣的:(Java,java,jmap,jcmd)