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

先直接给出结论:

#1

jcmd <pid> GC.class_histogram

或 #2

jmap -histo <pid>

 或 #3

jmap -histo:live <pid>

执行结果如下所示:

 num     #instances         #bytes  class name (module)
-------------------------------------------------------
   1:        476831       43623368  [B (java.base@16.0.1)
   2:        176012       21862544  [C (java.base@16.0.1)
   3:        571102       18275264  java.util.HashMap$Node (java.base@16.0.1)
   4:        666997       16007928  java.lang.String (java.base@16.0.1)
   5:        151332       11937928  [I (java.base@16.0.1)
   6:        117792        9024648  [Ljava.util.HashMap$Node; (java.base@16.0.1)
   7:        140910        8463264  [Ljava.lang.Object; (java.base@16.0.1)
   8:         68296        6286424  [J (java.base@16.0.1)
   9:         92323        4431504  java.util.HashMap (java.base@16.0.1)
  10:         87662        3506480  java.util.LinkedHashMap$Entry (java.base@16.0.1)
  11:         25716        3158832  java.lang.Class (java.base@16.0.1)
  12:         58626        2739904  [Ljava.lang.String; (java.base@16.0.1)
  13:        104695        2512680  java.util.ArrayList (java.base@16.0.1)
  14:         25001        2400096  org.apache.maven.model.Dependency
  15:         66238        2119616  java.util.Collections$UnmodifiableMap (java.base@16.0.1)
  16:         35971        2014376  java.util.LinkedHashMap (java.base@16.0.1)
  17:         27768        1999296  org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImpl
  18:         53012        1696384  org.apache.maven.model.InputLocation
  19:         16823        1586224  [[C (java.base@16.0.1)
  20:         34742        1389680  org.eclipse.aether.artifact.DefaultArtifact
  21:         42643        1364576  java.util.concurrent.ConcurrentHashMap$Node (java.base@16.0.1)
  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 (java.base@16.0.1)
  26:         25816        1032640  java.util.WeakHashMap$Entry (java.base@16.0.1)
  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 (java.base@16.0.1)
  30:         33835         812040  jdk.internal.jimage.ImageLocation
  31:         29809         715416  jdk.internal.jimage.ImageLocation
  32:          1597         675072  [Ljava.util.concurrent.ConcurrentHashMap$Node; (java.base@16.0.1)
  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 (java.base@16.0.1)
  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; (java.base@16.0.1)
  44:          1160         417440  [[Ljava.lang.Object; (java.base@16.0.1)
  45:         17894         405280  [Ljava.lang.Class; (java.base@16.0.1)
  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 (java.base@16.0.1)

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

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

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

jmap -histo <pid> | grep log4j

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

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

Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>
   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 <command>'.

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

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

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

java <app_name> -verbose:class

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

java <app_name> -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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值