msm(CentOS 6)及jvm虚拟机性能监控(04)_recv
為什么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
| 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这么全的数组去重,你怕不怕?
- 下一篇: Scala Implicit