为什么80%的码农都做不了架构师?>>>   

JVM

JVM内存管理--运行时数据区


JVM大体由五个部分组成,分别为JVM Stack、Native Stack、Program Counter Register、Java Heap、Method Area。其中JVM Stack、Native Stack、Program Counter Register为每个线程私有,然而Java Heap、Method Area为所有线程共享。每个对象都是由它对应的内部数据和外置的、可以由类提供共享给各个对象的各个方法组成。方法是外部代码,由每个实例化出来的对象可以调用的,而 Java Heap主要是用来存放这些对象的,对于JVM来说,Java Heap所占据的空间最大。

两类JVM管理的内存

JVM管理的内存段可以分为两大类:线程共享内存和线程私有内存。

线程共享内存

  • Method Area : 存储jvm加载的class、常量、静态变量、即时编译器编译后的代码等。
  • Java Heap :存储java的所有对象实例、数组等。

线程私有内存

  • Program Counter Register : 每个线程都有自己的计数寄存器,存储当前线程执行字节码的地址。
  • JVM Stack : jvm会为每个运行线程分配一个栈区,线程调用方法时和方法返回时会进行入栈和出栈操作。
  • Native Stack : 与 jvm stack 类似,只不过此区域是为调用本地方法服务。

Java Heap Size Options

http://docs.oracle.com/cd/E13222_01/wls/docs81/perform/JVMTuning.html

Task Option Recommended Value
Setting the New generation heap size -XX:NewSize

Set this value to a multiple of 1024 that is greater than 1MB. As a general rule, set -XX:NewSize to be one-fourth the size of the maximum heap size. Increase the value of this option for larger numbers of short-lived objects.

Be sure to increase the New generation as you increase the number of processors. Memory allocation can be parallel, but garbage collection is not parallel.

Setting the maximum New generation heap size -XX:MaxNewSize Set this value to a multiple of 1024 that is greater than 1MB
Setting New heap size ratios -XX:SurvivorRatio

The New generation area is divided into three sub-areas: Eden, and two survivor spaces that are equal in size.

Configure the ratio of the Eden/survivor space size. Try setting this value to 8, and then monitor your garbage collection.

Setting minimum heap size -Xms Set the minimum size of the memory allocation pool. Set this value to a multiple of 1024 that is greater than 1MB. As a general rule, set minimum heap size (-Xms) equal to the maximum heap size (-Xmx) to minimize garbage collections.
Setting maximum heap size -Xmx Set the maximum size of the memory allocation pool. Set this value to a multiple of 1024 that is greater than 1MB.
参数 说明
-Xmx young generation 和 old generation总共可用的最大空间
-Xms young generation 和 old genertaion二者初始空间。没分配的叫做reserved,再没有的话,向内核申请,如果内核没有内存的话,想办法使用LRU算法从其他程序里腾出内存给它。
-XX:NewSize young generation初始空间
-XX:MaxNewSize young generation最大空间
-XX:PermSize permanent generation初始空间
-XX:MaxPermSize permanent generation最大空间

参数 -Xmx 和 -Xms 经常使用,使用的方式是,在jvm启动时将该参数及其对应的值传递给jvm,具体合适传递哪?一般来讲,启动tomcat的话,在catalina.sh中有两个环境变量:CATALINA_OPTS、JAVA_OPTS。CATALINA_OPTS仅对启动运行tomcat实例的jvm虚拟机有效。JAVA_OPTS对本机上的所有JVM有效。如果机器上不仅仅有tomcat实例,建议使用CATALINA_OPTS。可以直接编辑catalina.sh,也可以使用命令设定,例如:

$ export CALALINA_OPTS="-Xmx256m"

The memory structure of a JVM process

对于Java Heap而言,分为 young generation、old generation、permanent generation三个区域。young generation又可分为 to、from、eden三个子区域,之所以分成这三个子区域主要原因是在young generation上面的垃圾收集算法或者叫垃圾收集器,会分别实现young generation、old generation、permanent generation三个区域移动对象之后,完成gc。创建的对象在young generation中过了一定存活时间以后,依然被采用的,那么该对象就会从young generation挪到old generation。如果有些对象创建以后不会被删除的,那么该对象就会被存放在permanent generation。

JVM Memory Handling

  • When the JVM starts, the host OS assigns a dedicated( 专注的,投入的; 献身的; 专用的;) memory space to that VM【vm会从操作系统那里取到一定内存空间】
  • The VM allocates memory to the application within that dedicated memory space【vm会按照特定的格式划分区段,之所以划分是为了让垃圾回收器完成垃圾回收】
  • The VM frees memory automatically via(经过; 通过,凭借; 取道;) garbage collectors
  • Garbage Collection is an expensive(昂贵的,花钱多的; 豪华的;) algorithm

Garbage Collectors

如果不考虑permanent generation的话,内存区域大体可以分为以下三个部分:

  • Eden Space: where objects ara born
  • Survivor Spaces : Where objects mature(成熟),contains to, from。
  • Tenured Space : Where objects grow old and die

其中 Eden Space 和 Survivor组成的是young generation。Tenured Space表示old generation。

JVM Memory Layout

New/Young - Recently created object

Old - Long lived object

Perm - JVM classes and methods

Garbage Collector

  • Collecting unsed java object
  • Cleaning memory
  • Minor GC : Collection memory in New/Young generation
  • Major GC( Full GC ) : Collection memory in old generation

young generation和old generation的gc的频率不同,算法也不同。不断地执行垃圾回收,腾出空间,主要是针对young generation实现的,对young generation实现垃圾回收的叫做Minor GC,对old generation实现垃圾回收的叫做Major GC。java当中垃圾回收器本身有多种实现方案,即便是Minor GC、Major GC。

Minor GC

注意垃圾回收不是实时进行的,而是积攒到一定的量才会进行操作。执行垃圾回收的过程中其他的线程无法工作,因为其非常消耗资源,会占据整个cpu,这种垃圾回收算法也称之为stop the world,这也就是我们日常生活中使用andriod手机假死的原因。与此不同的苹果ios,是用object-c开发的,程序直接运行在硬件之上,其一:性能好,不用虚拟机解释、运行代码;其二:object-c自己可以实现内存管理,内存回收是及时的,不存在垃圾回收的过程。

Major GC

  • Old Generation
  • Mark and compact
  • Slow ( 1st - goes through the entire heap, marking unreachable objects. 2nd - unreachable objects are compacted )。

对于old generation的垃圾回收一般用Mark and compact( 标记清除算法)实现的,这种垃圾回收的工作恒比较慢,分为两个阶段,第一个步要遍历整个堆内存,标记不再使用的对象,第二步将不再使用的对象一起打包一次性回收。Major GC发生的过程才是真正的stop the world发生的过程,一般发声Major GC原因是old generation 中的对象过多,而大多数的java程序发生Major GC 很少,因为对象被送到old generation区域的少之又少,在到达old generation之前已经被回收了。一旦发声full gc,就需要等待该过程完成,可能也需要手动释放一些堆空间。

根据业务运行模型和内存使用方式来指定不同区域的大小。

性能监控工具

常见问题:OutOfMemoryError:内存不足。引发该问题的原因有内存泄漏(代码问题)、线程死锁、锁竞争(Lock Contention)、Java消耗过多的CPU等等。

jps

jps( java machine process status tool )

监控jvm进程状态信息。jps本身也是使用java开发的,所以它本身也运行这一个java进程。

jps [options] [hostid]-m :输出传入main方法的参数-l :显示main类或者jar文件的完全限定名称(也就是完整类名)-v :显示jvm指定的参数(也就是启动jvm的时候传递了哪些参数给它) 
[root@app1 ~]# jps
3808 Jps
784 Bootstrap
[root@app1 ~]# jps -m -l # 查看真正启动jvm的程序或者类是什么
3730 sun.tools.jps.Jps -m -l
784 org.apache.catalina.startup.Bootstrap start
[root@app1 ~]#
[root@app1 ~]# jps -m -l -v
784 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/usr/soft/apache-tomcat-7.0.76/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms2048m -Xmx32768m -Xss4096K -XX:PermSize=1024m -XX:MaxPermSize=2048m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/soft/apache-tomcat-7.0.76/endorsed -Dcatalina.base=/usr/soft/apache-tomcat-7.0.76 -Dcatalina.home=/usr/soft/apache-tomcat-7.0.76 -Djava.io.tmpdir=/usr/soft/apache-tomcat-7.0.76/temp
4056 sun.tools.jps.Jps -m -l -v -Denv.class.path=.:/usr/jdk64/jdk1.7.0_67/lib/dt.jar:/usr/jdk64/jdk1.7.0_67/lib/tools.jar -Dapplication.home=/usr/jdk64/jdk1.7.0_67 -Xms8m
[root@app1 ~]# 

jstack

用于查看某个java进程内的线程堆栈信息。

jstack [options] pid-l long listings 输出完整的锁信息。可以指定输出某一项。-m 混合模式,即会输出java堆栈及c/c++堆栈信息

哪一个java进程最耗cpu? top->jstack

jmap

jmap (jvm mamory map) :查看堆内存使用情况。

jmap [options] pid-heap :详细输出堆内存空间使用状态信息-histo[:live] :查看堆内存中的对象数目、大小统计结果。
[root@app1 ~]# jmap
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-permstat            to print permanent generation statistics-finalizerinfo       to print information on objects awaiting finalization-dump:<dump-options> to dump java heap in hprof binary formatdump-options:live         dump only live objects; if not specified,all objects in the heap are dumped.format=b     binary formatfile=<file>  dump heap to <file>Example: jmap -dump:live,format=b,file=heap.bin <pid>-F                   force. Use with -dump:<dump-options> <pid> or -histoto force a heap dump or histogram when <pid> does notrespond. The "live" suboption is not supportedin this mode.-h | -help           to print this help message-J<flag>             to pass <flag> directly to the runtime system
[root@app1 ~]# jmap 784
Attaching to process ID 784, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.65-b04
0x0000000000400000      7K      /usr/jdk64/jdk1.7.0_67/bin/java
0x00000031f8400000      153K    /lib64/ld-2.12.so
0x00000031f8800000      22K     /lib64/libdl-2.12.so
0x00000031f8c00000      1883K   /lib64/libc-2.12.so
0x00000031f9000000      142K    /lib64/libpthread-2.12.so
0x00000031f9400000      46K     /lib64/librt-2.12.so
0x00000031f9800000      585K    /lib64/libm-2.12.so
0x00000031fa800000      91K     /lib64/libgcc_s-4.4.7-20120601.so.1
0x00000031fd800000      111K    /lib64/libresolv-2.12.so
0x00007f28b41eb000      26K     /lib64/libnss_dns-2.12.so
0x00007f28bc172000      477K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libt2k.so
0x00007f2934178000      512K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libfontmanager.so
0x00007f293c1ea000      36K     /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/headless/libmawt.so
0x00007f297c11f000      755K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libawt.so
0x00007f29e0bf2000      250K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libsunec.so
0x00007f29e123a000      44K     /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libmanagement.so
0x00007f29e1442000      112K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libnet.so
0x00007f29e1659000      89K     /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libnio.so
0x00007f331872f000      120K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libzip.so
0x00007f331894a000      64K     /lib64/libnss_files-2.12.so
0x00007f3318b61000      214K    /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libjava.so
0x00007f3318d8c000      63K     /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/libverify.so
0x00007f331939b000      14853K  /usr/jdk64/jdk1.7.0_67/jre/lib/amd64/server/libjvm.so
0x00007f331a212000      103K    /usr/jdk64/jdk1.7.0_67/lib/amd64/jli/libjli.so
[root@app1 ~]# jmap -heap 784
Attaching to process ID 784, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.65-b04using thread-local object allocation.
Parallel GC with 18 thread(s)  # 垃圾收集算法Heap Configuration:MinHeapFreeRatio = 0MaxHeapFreeRatio = 100MaxHeapSize      = 34359738368 (32768.0MB)NewSize          = 1310720 (1.25MB)MaxNewSize       = 17592186044415 MBOldSize          = 5439488 (5.1875MB)NewRatio         = 2SurvivorRatio    = 8PermSize         = 1073741824 (1024.0MB)MaxPermSize      = 2147483648 (2048.0MB)G1HeapRegionSize = 0 (0.0MB)Heap Usage:
PS Young Generation
Eden Space:capacity = 4660920320 (4445.0MB)used     = 3455457416 (3295.380989074707MB)free     = 1205462904 (1149.619010925293MB)74.13680515353671% used
From Space:capacity = 6815744 (6.5MB)used     = 6811632 (6.4960784912109375MB)free     = 4112 (0.0039215087890625MB)99.93966909555289% used
To Space:capacity = 143130624 (136.5MB)used     = 0 (0.0MB)free     = 143130624 (136.5MB)0.0% used
PS Old Generationcapacity = 1431830528 (1365.5MB)used     = 275154128 (262.4074249267578MB)free     = 1156676400 (1103.0925750732422MB)19.216947999030232% used
PS Perm Generationcapacity = 1073741824 (1024.0MB)used     = 91206560 (86.98135375976562MB)free     = 982535264 (937.0186462402344MB)8.494272828102112% used42967 interned Strings occupying 5456928 bytes.
[root@app1 ~]# jmap -histo:live 784 # 详细显示java堆空间中的每一个对象num     #instances         #bytes  class name
----------------------------------------------1:        238261       31749640  [C2:         48744       30171672  [B3:        152303       23372728  <constMethodKlass>4:        152303       19508224  <methodKlass>5:         13376       16932432  <constantPoolKlass>6:         13376       10056128  <instanceKlassKlass>7:         10801        9292768  <constantPoolCacheKlass>8:        233736        7479552  java.lang.String9:         43674        4785504  [Ljava.lang.Object;10:          6485        3820120  <methodDataKlass>11:         25037        3605328  java.lang.reflect.Method12:         14301        3046232  java.lang.Class13:         47576        3044864  java.util.LinkedHashMap$Entry14:         13333        2737448  [Ljava.util.HashMap$Entry;15:         54334        2608032  java.util.concurrent.ConcurrentHashMap$HashEntry16:          4380        2191472  [I17:         21963        2021432  [[I18:         38640        1854720  java.util.HashMap$Entry19:         17256        1794624  java.net.URL20:         18236        1309960  [S21:         14735        1296680  java.util.LinkedHashMap22:         30648        1225920  java.util.ArrayList23:          5079        1066472  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;24:         14417        1038024  org.apache.xerces.dom.DeferredTextImpl25:         12418         894096  org.apache.catalina.loader.ResourceEntry26:         14042         835776  [Ljava.lang.String;27:          6231         797568  org.apache.jasper.compiler.Node$TemplateText28:          7247         753688  org.apache.xerces.dom.DeferredElementImpl29:          7749         681912  org.apache.jasper.compiler.Mark30:         13745         659760  java.lang.ref.WeakReference31:         10086         645504  org.apache.xerces.dom.DeferredAttrImpl32:         11230         628880  java.lang.ref.SoftReference33:          8399         604728  java.util.HashMap
。。。省略

jhat

jhat (jvm heap analysis tool),更多用于代码中的运行状况。

[root@app1 ~]# jhat
ERROR: No arguments supplied
Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>-J<flag>          Pass <flag> directly to the runtime system. Forexample, -J-mx512m to use a maximum heap size of 512MB-stack false:     Turn off tracking object allocation call stack.-refs false:      Turn off tracking of references to objects-port <port>:     Set the port for the HTTP server.  Defaults to 7000-exclude <file>:  Specify a file that lists data members that shouldbe excluded from the reachableFrom query.-baseline <file>: Specify a baseline object dump.  Objects inboth heap dumps with the same ID and same class willbe marked as not being "new".-debug <int>:     Set debug level.0:  No debug output1:  Debug hprof file parsing2:  Debug hprof file parsing, no server-version          Report version number-h|-help          Print this help and exit<file>            The file to readFor a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".All boolean options default to "true"
[root@app1 ~]# 

jstat

jstat (jvm统计监测工具)

[root@app1 ~]# jstat
invalid argument count
Usage: jstat -help|-optionsjstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]  # option为必选项Definitions:<option>      An option reported by the -options option<vmid>        Virtual Machine Identifier. A vmid takes the following form:<lvmid>[@<hostname>[:<port>]]Where <lvmid> is the local vm identifier for the targetJava virtual machine, typically a process id; <hostname> isthe name of the host running the target Java virtual machine;and <port> is the port number for the rmiregistry on thetarget host. See the jvmstat documentation for a more completedescription of the Virtual Machine Identifier.<lines>       Number of samples between header lines.<interval>    Sampling interval. The following forms are allowed:<n>["ms"|"s"]Where <n> is an integer and the suffix specifies the units as milliseconds("ms") or seconds("s"). The default units are "ms".<count>       Number of samples to take before terminating.-J<flag>      Pass <flag> directly to the runtime system.
[root@app1 ~]# jstat -gc 784  S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT
129024.0 512.0   0.0    0.0   4153856.0 61658.6  1398272.0   120259.0  1048576.0 88442.2     23    1.005   2      0.641    1.646
[root@app1 ~]# 
S0C,S1C,S0U,S1U:C表示容量,U表示已用量。
EC,EU:eden区域的容量和已用量。
OC,OU
PC,PU
YGC,YGT:表示新生代的gc次数和耗时
FGC,FGCT:表示FULL GC的次数和耗时
GCT:GC总耗时

jconsole

jvisualvm

转载于:https://my.oschina.net/qzhli/blog/903399

msm(CentOS 6)及jvm虚拟机性能监控(04)_recv相关推荐

  1. jvm(4)-虚拟机性能监控与故障处理工具

    [0]README 0.1)本文文字描述转自 "深入理解jvm",旨在了解 虚拟机性能监控与故障处理工具的基础知识(仅仅在于了解): [1]概述 1)给一个系统定位问题的时候,知识 ...

  2. JVM(三).虚拟机性能监控故障处理工具

    JVM (三).虚拟机性能监控&故障处理工具 1.概述 系统定位问题,需要工具来导出出现问题的数据:这里的数据有异常堆栈,虚拟机日志,垃圾回收日志,线程快照,堆快照文件:工具就很多就是虚拟机自 ...

  3. JVM-11虚拟机性能监控与故障处理工具之【JDK的可视化工具-JConsole】

    文章目录 思维导图 概述 JConsole: Java监视与管理平台 启动jconsole 内存监控示例 VM ARGS 代码 JConsole监控展示及说明 扩展问题 没有指定-Xmn,如何确定新生 ...

  4. JVM-10虚拟机性能监控与故障处理工具之【JDK的命令行】

    文章目录 思维导图 概述 命令行工具 jps 虚拟机进程状况工具 概述 语法及使用 jstat 虚拟机统计信息监视工具 概述 语法及使用 jinfo Java配置信息工具 概述 语法及使用 jmap ...

  5. JYM虚拟机性能监控与故障处理工具

    虚拟机性能监控与故障处理工具 一.jps:虚拟机进程状况工具 常用指令 二.jstat:虚拟机统计信息监视工具 常用指令 三.jinfo:配置信息工具 四.jmap:Java内存映像工具 常用指令 五 ...

  6. 虚拟机性能监控与故障处理工具

    虚拟机性能监控与故障处理工具 概述 给一个系统定位问题时,知识.经验是关键基础,数据是依据,工具是运用知识处理数据的手段.这里的数据包括:运行日志.异常堆栈.GC日志.线程快照.堆转储快照等.使用适当 ...

  7. 《深入理解Java虚拟机第3版》垃圾收集器与内存分配策略、虚拟机性能监控故障处理工具

    目录 往期博客:Java课堂篇3_初识JMM.常量池简单理解(字符串常量池.静态常量池.大整型常量池) 为什么要了解垃圾收集和内存分配? 如何判断对象已死? 引用计数算法 可达性分析算法 JDK1.2 ...

  8. 《深入理解JVM.2nd》笔记(四):虚拟机性能监控与故障处理工具

    文章目录 概述 JDK的命令行工具 jps:虚拟机进程状况工具 jstat:虚拟机统计信息监视工具 jinfo:Java配置信息工具 jmap:Java内存映像工具 jhat:虚拟机堆转储快照分析工具 ...

  9. 深入理解JAVA虚拟机 虚拟机性能监控和故障处理工具

    jre的bin目录下的工具,都非常小.它都是tools.jar下面的代码的一层封装而已.tools.jar不是java标准,是Hotspot实现的. 名称 作用 jps JVM Process Sta ...

最新文章

  1. Activiti——工作流之流程实例、任务的执行(五)
  2. mysql游标示例mysql游标简易教程
  3. 从“诺奖级”成果到“非主观造假”,时隔6年,韩春雨带着原一作,再发高分文章!...
  4. Leetcode-探索 | 加一
  5. Service IntentService区别 (面试)
  6. Spring+XFire WS-Security安全认证开发感悟
  7. 【Android 异步操作】Handler 机制 ( MessageQueue 消息队列的阻塞机制 | Java 层机制 | native 层阻塞机制 | native 层解除阻塞机制 )
  8. [BZOJ5249][九省联考2018]IIIDX(线段树)
  9. linux ba 模拟,在你的 Python 游戏中模拟引力 | Linux 中国
  10. idea关闭页面显示的浏览器图标
  11. 基于图像识别的火灾检测系统设计思路流程
  12. 定制WES7紧急修复U盘
  13. 时间序列数据如何插补缺失值?
  14. html格式转换word清除格式,word文档如何清除格式(原来Word可以一键去除格式)...
  15. bilibili、腾讯视频下载方法及过程中遇到的一些问题
  16. css 平行四边形 梯形 组合_CSS-实现三角形、梯形、平行四边形、圆形、椭圆形、对话框、自适应正方形 Web程序 - 贪吃蛇学院-专业IT技术平台...
  17. 用计算机绘图课件,第7章 计算机绘图ppt课件.ppt
  18. 批量处理:读取文件夹,将json文件转化为txt文件
  19. java读txt文件乱码_java读取txt文件时出现中文乱码怎么解决
  20. b站视频突破2倍方法,3倍?4倍?可以开10倍!!!

热门文章

  1. 组合恒等式3 母函数与形式幂级数的运算
  2. UA MATH564 概率论 QE练习题3
  3. Wireshark EndPoints窗口
  4. Win32 鼠标绘图代码研究
  5. Vi编辑器中查找替换
  6. 你大概走了假敏捷:认真说说敏捷的实现和问题(手绘版)
  7. .Net 4.0 (2)
  8. MVC应用程序实现上传文件(续)
  9. ADS的go to命令
  10. springboot oauth2登录成功处理器_Spring Boot Security 整合 OAuth2 设计安全API接口服务...