波神

导航

jvm参数与GC

一、JVM的新生代、老年代、与永久代

JVM中的堆,一般分为三大部分:新生代、老年代、永久代:

1、新生代:主要是用来存放新生的对象,一般占据堆的1/3空间。由于频繁创建对象,所以新生代会频繁触发MinorGC进行垃圾回收。新生代分为以下三个区:

  •          Eden区:Java新对象的出生地(如果新创建的对象占用内存很大,则直接分配到老年代)。当Eden区内存不够的时候就会触发MinorGC,对新生代区进行一次垃圾回收。
  •          ServivorTo:保留了一次MinorGC过程中的幸存者。
  •          ServivorFrom:上一次GC的幸存者,作为这一次GC的被扫描者。
MinorGC采用复制算法。
首先,把Eden和ServivorFrom区域中存活的对象复制到ServicorTo区域(如果有对象的年龄以及达到了老年的标准,则赋值到老年代区),同时把这些对象的年龄+1(如果ServicorTo不够位置了就放到老年区);

然后,清空Eden和ServicorFrom中的对象;最后,ServicorTo和ServicorFrom互换,原ServicorTo成为下一次GC时的ServicorFrom区。

2、老年代:主要存放应用程序中生命周期长的内存对象,老年代的对象比较稳定,所以MajorGC不会频繁执行。在进行MajorGC前一般都先进行了一次MinorGC,使得有新生代的对象晋身入老年代,导致空间不够用时才触发。当无法找到足够大的连续空间分配给新创建的较大对象时也会提前触发一次MajorGC进行垃圾回收腾出空间。

MajorGC采用标记—清除算法:
首先扫描一次所有老年代,标记出存活的对象,然后回收没有标记的对象。MajorGC的耗时比较长,因为要扫描再回收。MajorGC会产生内存碎片,为了减少内存损耗,我们一般需要进行合并或者标记出来方便下次直接分配。

3、永久代:指内存的永久保存区域,主要存放Class和Meta(元数据)的信息,Class在被加载的时候被放入永久区域. 它和和存放实例的区域不同,GC不会在主程序运行期对永久区域进行清理。所以这也导致了永久代的区域会随着加载的Class的增多而胀满,最终抛出OOM异常。

    在Java8中,永久代已经被移除,被一个称为“元数据区”(元空间)的区域所取代。

    元空间的本质和永久代类似,都是对JVM规范中方法区的实现。不过元空间与永久代之间最大的区别在于:元空间并不在虚拟机中,而是使用本地内存。因此,默认情况下,元空间的大小仅受本地内存限制。类的元数据放入 native memory, 字符串池和类的静态变量放入java堆中. 这样可以加载多少类的元数据就不再由      MaxPermSize控制, 而由系统的实际可用空间来控制.

4、MinorGC Vs FullGC

Major GC 是清理OldGen。
Full GC 是清理整个堆空间—包括年轻代和永久代。
Full GC触发条件:
1、老生代空间不够分配新的内存
2System.gc()
3、CMS的Concurrent-Mode-Failure
4、新生代晋升的平均大小 大于 老年代的剩余空间 (为了避免新生代晋升到老年代失败)

扩展:CMS,全称Concurrent Low Pause Collector,是jdk1.4后期版本开始引入的新gc算法,在jdk5和jdk6中得到了进一步改进,它的主要适合场景是对响应时间的重要性需求 大于对吞吐量的要求,能够承受垃圾回收线程和应用线程共享处理器资源,并且应用中存在比较多的长生命周期的对象的应用。CMS是用于对tenured generation的回收,也就是年老代的回收,目标是尽量减少应用的暂停时间,减少full gc发生的几率,利用和应用程序线程并发的垃圾回收线程来标记清除年老代。在我们的应用中,因为有缓存的存在,并且对于响应时间也有比较高的要求,因此希 望能尝试使用CMS来替代默认的server型JVM使用的并行收集器,以便获得更短的垃圾回收的暂停时间,提高程序的响应性。

CMS并非没有暂停,而是用两次短暂停来替代串行标记整理算法的长暂停,它的收集周期是这样:
初始标记(CMS-initial-mark) -> 并发标记(CMS-concurrent-mark) -> 重新标记(CMS-remark) -> 并发清除(CMS-concurrent-sweep) ->并发重设状态等待下次CMS的触发(CMS-concurrent-reset)。
其中的1,3两个步骤需要暂停所有的应用程序线程的。第一次暂停从root对象开始标记存活的对象,这个阶段称为初始标记;第二次暂停是在并发标记之后, 暂停所有应用程序线程,重新标记并发标记阶段遗漏的对象(在并发标记阶段结束后对象状态的更新导致)。第一次暂停会比较短,第二次暂停通常会比较长,并且 remark这个阶段可以并行标记。

而并发标记、并发清除、并发重设阶段的所谓并发,是指一个或者多个垃圾回收线程和应用程序线程并发地运行,垃圾回收线程不会暂停应用程序的执行,如果你有多于一个处理器,那么并发收集线程将与应用线程在不同的处理器上运行,显然,这样的开销就是会降低应用的吞吐量。Remark阶段的并行,是指暂停了所有应用程序后,启动一定数目的垃圾回收进程进行并行标记,此时的应用线程是暂停的。



二、JVM和GC基本信息查看 

1、jmap -heap

[root@7aa1c9ceeee5 logs]# jmap -heap 30             
Attaching to process ID 30, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.51-b03

using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC

Heap Configuration:
   MinHeapFreeRatio         = 40 #最小堆使用用比例,可以用  -XX:MinHeapFreeRatio=<n>参数设置
   MaxHeapFreeRatio         = 70 #最大堆可用比例,可以用 -XX:MaxHeapFreeRatio=<n>参数设置
   MaxHeapSize              = 2147483648 (2048.0MB)  #最大堆空间大小,可以用 -XX:MaxHeapSize=<n>参数设置
   NewSize                  = 697892864 (665.5625MB)  #新生代分配大小,可以用   可由-XX:NewSize=<n>参数设置 
   MaxNewSize               = 697892864 (665.5625MB)  #新生代最大可分配大小,可以用 -XX:MaxNewSize=<n>参数设置
   OldSize                  = 1449590784 (1382.4375MB) #老年代大小,可以用  -XX:OldSize=<n>参数设置
   NewRatio                 = 2  #新生代比例,表示年轻代与年老代所占比值为1:2,年轻代占整个堆栈的1/3,可以用 -XX:NewRatio=<n>参数
   SurvivorRatio            = 8  #Eden区与Survivor区的大小比值,设置为8,则两个Survivor区与一个Eden区的比值为2:8,一个Survivor区占整个年轻代的1/10,可以用  -XX:SurvivorRatio=<n>设置
   MetaspaceSize            = 21807104 (20.796875MB)   #元空间的默认大小,超过此值就会触发Full GC,可以用   -XX:MetaspaceSize=<n>参数设置
   CompressedClassSpaceSize = 1073741824 (1024.0MB)  #类指针压缩空间的默认大小 可由-XX:CompressedClassSpaceSize=<n>参数设置
   MaxMetaspaceSize         = 17592186044415 MB  #元空间的最大大小 可由-XX:MaxMetaspaceSize=<n>参数设置
   G1HeapRegionSize         = 0 (0.0MB)  #使用G1垃圾收集器的时候,堆被分割的大小 可由-XX:G1HeapRegionSize=<n>参数设置

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 628162560 (599.0625MB)
   used     = 469751768 (447.99019622802734MB)
   free     = 158410792 (151.07230377197266MB)
   74.78187939122% used
Eden Space:
   capacity = 558432256 (532.5625MB)
   used     = 455338072 (434.24422454833984MB)
   free     = 103094184 (98.31827545166016MB)
   81.53864091976807% used
From Space:
   capacity = 69730304 (66.5MB)
   used     = 14413696 (13.7459716796875MB)
   free     = 55316608 (52.7540283203125MB)
   20.670634104793233% used
To Space:
   capacity = 69730304 (66.5MB)
   used     = 0 (0.0MB)
   free     = 69730304 (66.5MB)
   0.0% used
concurrent mark-sweep generation:  #老年代使用情况
   capacity = 1449590784 (1382.4375MB)
   used     = 8050969119943769528 (7.678002471870202E12MB)
   free     = 9914183573928 MB
   5.553959923591628E11% used

35048 interned Strings occupying 3885448 bytes.

 2、-histo[:live] 打印每个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量. 

3、gc的使用情况:

 1)jstat -gc [-h3每3行显示标题] <pid> [多少毫秒eg5000]

S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
S0U: Survivor space 0 utilization (kB).
S1U: Survivor space 1 utilization (kB).
EC: Current eden space capacity (kB).
EU: Eden space utilization (kB).
OC: Current old space capacity (kB).
OU: Old space utilization (kB).
MC: Metaspace capacity (kB).
MU: Metacspace utilization (kB).
CCSC: Compressed class space capacity (kB).
CCSU: Compressed class space used (kB).
YGC: Number of young generation garbage collection events.
YGCT: Young generation garbage collection time.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.

2)jstat -gccapacity pid 

可以显示,jvm内存中三代(young,old,perm)对象的使用和占用大小

[root@7aa1c9ceeee5 ~]# jstat -gccapacity 30
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC 
681536.0 681536.0 681536.0 68096.0 68096.0 545344.0  1415616.0  1415616.0  1415616.0  1415616.0      0.0 1107968.0  65384.0      0.0 1048576.0   7340.0     86     7
NGCMN: Minimum new generation capacity (kB).
NGCMX: Maximum new generation capacity (kB).
NGC: Current new generation capacity (kB).
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
EC: Current eden space capacity (kB).
OGCMN: Minimum old generation capacity (kB).
OGCMX: Maximum old generation capacity (kB).
OGC: Current old generation capacity (kB).
OC: Current old space capacity (kB).
MCMN: Minimum metaspace capacity (kB).
MCMX: Maximum metaspace capacity (kB).
MC: Metaspace capacity (kB).
CCSMN: Compressed class space minimum capacity (kB).
CCSMX: Compressed class space maximum capacity (kB).
CCSC: Compressed class space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.

3) jstat -gcutil pid 

统计gc信息统计

[root@7aa1c9ceeee5 ~]# jstat -gcutil 30 
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  20.27  81.21   5.71  97.02  94.08     87    1.986     7    0.932    2.917
S0: Survivor space 0 utilization as a percentage of the space's current capacity.
S1: Survivor space 1 utilization as a percentage of the space's current capacity.
E: Eden space utilization as a percentage of the space's current capacity.
O: Old space utilization as a percentage of the space's current capacity.
M: Metaspace utilization as a percentage of the space's current capacity.
CCS: Compressed class space utilization as a percentage.
YGC: Number of young generation GC events.
YGCT: Young generation garbage collection time.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.

4) jstat -gcnew pid   年轻代对象的信息

[root@7aa1c9ceeee5 ~]# jstat -gcnew 30 
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
68096.0 68096.0 15142.4    0.0 15  15 34048.0 545344.0 260437.4     88    1.999
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
S0U: Survivor space 0 utilization (kB).
S1U: Survivor space 1 utilization (kB).
TT: Tenuring threshold.
MTT: Maximum tenuring threshold.
DSS: Desired survivor size (kB).
EC: Current eden space capacity (kB).
EU: Eden space utilization (kB).
YGC: Number of young generation GC events.
YGCT: Young generation garbage collection time.

5)jstat -gcnewcapacity pid  年轻代对象的信息及其占用量

[root@7aa1c9ceeee5 ~]# jstat -gcnewcapacity 30 
  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC 
  681536.0   681536.0   681536.0  68096.0  68096.0  68096.0  68096.0   545344.0   545344.0    88     7
NGCMN: Minimum new generation capacity (kB).
NGCMX: Maximum new generation capacity (kB).
NGC: Current new generation capacity (kB).
S0CMX: Maximum survivor space 0 capacity (kB).
S0C: Current survivor space 0 capacity (kB).
S1CMX: Maximum survivor space 1 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
ECMX: Maximum eden space capacity (kB).
EC: Current eden space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.

6)jstat -gcold pid   老年代对象信息和元空间行为统计

[root@7aa1c9ceeee5 ~]# jstat -gcold 30  
   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   
 65384.0  63432.6   7340.0   6905.6   1415616.0     80877.2     88     7    0.932    2.931
MC: Metaspace capacity (kB).
MU: Metaspace utilization (kB).
CCSC: Compressed class space capacity (kB).
CCSU: Compressed class space used (kB).
OC: Current old space capacity (kB).
OU: Old space utilization (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.

7)jstat -gcoldcapacity pid   old代对象的信息及其占用量

[root@7aa1c9ceeee5 ~]# jstat -gcoldcapacity 30
   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT   
  1415616.0   1415616.0   1415616.0   1415616.0    89     7    0.932    2.948
OGCMN: Minimum old generation capacity (kB).
OGCMX: Maximum old generation capacity (kB).
OGC: Current old generation capacity (kB).
OC: Current old space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.

8) jstat -class pid  显示加载class的数量,及所占空间等信息

[root@7aa1c9ceeee5 ~]# jstat -class 30 
Loaded  Bytes  Unloaded  Bytes     Time   
 10563 19772.0       96   143.1       8.71
Loaded: Number of classes loaded.
Bytes: Number of kBs loaded.
Unloaded: Number of classes unloaded.
Bytes: Number of Kbytes unloaded.
Time: Time spent performing class loading and unloading operations.

9) jstat -compiler pid  显示VM实时编译的数量等信息

[root@7aa1c9ceeee5 ~]# jstat -compiler 30
Compiled Failed Invalid   Time   FailedType FailedMethod
   13800      1       0    83.96          1 org/springframework/core/SerializableTypeWrapper$TypeProxyInvocationHandler invoke
Compiled: Number of compilation tasks performed.
Failed: Number of compilations tasks failed.
Invalid: Number of compilation tasks that were invalidated.
Time: Time spent performing compilation tasks.
FailedType: Compile type of the last failed compilation.
FailedMethod: Class name and method of the last failed compilation.

10) jstat -printcompilation pid  当前VM执行的信息

[root@7aa1c9ceeee5 ~]# jstat -printcompilation 30 
Compiled  Size  Type Method
   13809    281    1 sun/util/locale/provider/LocaleServiceProviderPool hasProviders
Compiled: Number of compilation tasks performed by the most recently compiled method.
Size: Number of bytes of byte code of the most recently compiled method.
Type: Compilation type of the most recently compiled method.
Method: Class name and method name identifying the most recently compiled method. Class name uses slash (/) instead of dot (.) as a name space separator. Method name is the method within the specified class. The format for these two fields is consistent with the HotSpot -XX:+PrintCompilation option.

11) jstat -gcmetacapacity  元空间大小统计

[root@7aa1c9ceeee5 ~]# jstat -gcmetacapacity 30
   MCMN       MCMX        MC       CCSMN      CCSMX       CCSC     YGC   FGC    FGCT     GCT   
       0.0  1107968.0    65384.0        0.0  1048576.0     7340.0    89     7    0.932    2.948
MCMN: Minimum metaspace capacity (kB).
MCMX: Maximum metaspace capacity (kB).
MC: Metaspace capacity (kB).
CCSMN: Compressed class space minimum capacity (kB).
CCSMX: Compressed class space maximum capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.

4、jstack 查询某个进程的线程情况

1)查询运行中的线程

[root@7aa1c9ceeee5 ~]# jstack 30 | grep runnable                          
"ajp-nio-8009-Acceptor-0" #249 daemon prio=5 os_prio=0 tid=0x00007fd0b81b2000 nid=0x125 runnable [0x00007fcf8834e000]
"ajp-nio-8009-ClientPoller-1" #248 daemon prio=5 os_prio=0 tid=0x00007fd0b81b0000 nid=0x124 runnable [0x00007fcf8844f000]
"ajp-nio-8009-ClientPoller-0" #247 daemon prio=5 os_prio=0 tid=0x00007fd0b8357800 nid=0x123 runnable [0x00007fcf88550000]
"http-nio-8080-Acceptor-0" #246 daemon prio=5 os_prio=0 tid=0x00007fd0b8355800 nid=0x122 runnable [0x00007fcf88651000]
"http-nio-8080-ClientPoller-1" #245 daemon prio=5 os_prio=0 tid=0x00007fd0b8353800 nid=0x121 runnable [0x00007fd08034c000]
"http-nio-8080-ClientPoller-0" #244 daemon prio=5 os_prio=0 tid=0x00007fd0b8352800 nid=0x120 runnable [0x00007fd08044d000]
"AMQP Connection 172.16.0.155:5672" #71 prio=5 os_prio=0 tid=0x00007fcfd40f3800 nid=0x73 runnable [0x00007fcf93afb000]
"NioBlockingSelector.BlockPoller-2" #16 daemon prio=5 os_prio=0 tid=0x00007fd0b84f0000 nid=0x3c runnable [0x00007fd080b52000]
"NioBlockingSelector.BlockPoller-1" #15 daemon prio=5 os_prio=0 tid=0x00007fd0b84d5800 nid=0x3b runnable [0x00007fd080c53000]
"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x00007fd0b8147000 nid=0x37 runnable [0x0000000000000000]
"Signal Dispatcher" #5 daemon prio=9 os_prio=0 tid=0x00007fd0b8129000 nid=0x32 runnable [0x0000000000000000]
"main" #1 prio=5 os_prio=0 tid=0x00007fd0b8009800 nid=0x22 runnable [0x00007fd0be87c000]
"VM Thread" os_prio=0 tid=0x00007fd0b80eb800 nid=0x2e runnable 
"Gang worker#0 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b801a800 nid=0x23 runnable 
"Gang worker#1 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b801c800 nid=0x24 runnable 
"Gang worker#2 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b801e000 nid=0x25 runnable 
"Gang worker#3 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b8020000 nid=0x26 runnable 
"Gang worker#4 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b8022000 nid=0x27 runnable 
"Gang worker#5 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b8023800 nid=0x28 runnable 
"Gang worker#6 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b8025800 nid=0x29 runnable 
"Gang worker#7 (Parallel GC Threads)" os_prio=0 tid=0x00007fd0b8027800 nid=0x2a runnable 
"Concurrent Mark-Sweep GC Thread" os_prio=0 tid=0x00007fd0b8098800 nid=0x2d runnable 
"Gang worker#0 (Parallel CMS Threads)" os_prio=0 tid=0x00007fd0b8094800 nid=0x2b runnable 
"Gang worker#1 (Parallel CMS Threads)" os_prio=0 tid=0x00007fd0b8096800 nid=0x2c runnable

2)查询阻塞的线程

jstack 30 | grep blocked 

3)查询死忙的线程

jstack 30 | grep deadlock

 

三、JVM参数设置与日志分析

可以在JAVA_OPTS里增加jvm参数

JAVA_OPTS="$JAVA_OPTS -Xmx1500m -Xms1500m -XX:MaxNewSize=128m -XX:MaxPermSize=128m"

具体参数可以参照博主:http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html

GC日志分析,参照博主:https://www.cnblogs.com/duanxz/p/6092911.html

posted on 2019-02-27 18:21  波神  阅读(1221)  评论(0编辑  收藏  举报