1、JVM内存模型和垃圾收集,fanzhang 2009.08.31,意义何在,内存和垃圾收集对Java程序的性能起着至关重要的作用,特别是对服务器程序。 遭遇OOM?响应速度慢?并发上不去?吞吐量不够? 内存和垃圾收集是Java程序的OS。,遍布问号的底层,堆?:根集合?、链结构、老的居底、从顶分配、可达性、活跃对象 栈在哪里?:主内存和工作内存?栈空间也要配置? 引用:强引用和弱引用?垃圾收集不用引用计数用什么(无法回收:树、子指向父)?引用值会被改变?对象哈希值何时与之相关?,堆(Heap),JVM管理的内存叫堆(Heap);32位Linux最大3G;64位无限制 初始-Xms:默认1/64
2、但小于1G。 最大-Xmx:默认1/4但小于1G。-Xms与-Xms设置相同值,对服务端应用来说要给足空间(重要) 最小空余-XX:MinHeapFreeRatio=40%。 最大空余-XX:MaxHeapFreeRatio=70%。,堆的分代(Generation),堆分为三个代:Young、Old和Permanent,不同代采用不同的垃圾收集算法,以提升性能。(注:Permanent空间在大小上并没有计算在Heap空间之内)如下图:,年轻代(Young),年轻代采取复制收集算法。分为3个区域:一个Eden,所有新建对象;两个Survivor区,用来实施复制算法。 -XX:NewRatio=
3、Young:Old,推荐1:3-XX:NewSize和-XX:MaxNewSize设置相同值,适当放大,减少GC(重要) -XX:SurvivorRatio= Eden:1个Survivor,默认比例8,适当减小,防止老年化(重要),老年代(Old),年轻代的对象挺过数次收集,就会进入老年代。老年代使用标记整理算法。老年代存活时间长,不用复制算法。 -XX:MaxTenuringThreshold=年轻代熬过多少次收集后进入老年代。,持久代(Permanent),装载Class信息等基础数据 XX:MaxPermSize=64M,满了之后会引起一种不同类型的 OOM。类很多很多的程序,需要加大
4、之,一般使用默认值即可。,GC的类型,Minor Collection:对Young,复制算法,频率高。 Major Collection:同时对Young和Old,也叫Full GC;Old频率要比Young低很多,采用标记清除/标记整理算法;System.gc()可以建议引发之,使用-XX:+DisableExplicitGC禁止代码调用。,Young的GC频繁,范围小,速度快,称之为Minor Collection。 Eden+1Suvivor空间达阀值 - 移动对象到to space - to space与from space互换角色 - 对象移动次数到阀值就足够老了-Old。 Sur
5、vivor是Eden和Old的缓冲区,为Old把关,减少Old的大小和GC频次,Suvivor不足会Old化。 Old的GC范围广,较慢,频率低称之为Major Collection(Full Collection)。 先Young GC,再Old GC。,垃圾收集算法,复制(copying):将堆内分成两个相同空间,从根开始访问每一个关联的活跃对象。将空间A的活跃对象全部复制到空间B,然后一次性回收整个空间A。 只访问活跃对象,遍历空间的成本较小,但需要巨大的复制成本和较多的内存,需要修改引用值。如下图:,标记清除(mark-sweep):收集器先从根开始访问所有活跃对象,标记为活跃对象。然
6、后再遍历一次整个内存区域,把所有没有标记活跃的对象进行回收处理。遍历整个空间的成本较大、暂停时间随空间大小线性增大、整理后堆里的碎片很多。如下图:,标记整理(mark-sweep-compact):综合了上述两者的做法和优点,先标记活跃对象,然后将其合并成较大的内存块。如下图:,GC收集器类型,串行收集器(Serial Collector) -XX:+UseSerialGC:年轻代串行复制,老年代串行标记整理。(注:目前已不使用)如下图:,并行收集器(Throughput Collector) -XX:+UseParallelGC:JDK5.0以上-server(Linux的JDK默认是-se
7、rver)的默认值。年轻代:暂停应用程序,多个垃圾收集线程并行的复制收集,线程数默认为CPU个数,可用-XX:ParallelGCThreads= 设定线程数。 年老代:暂停应用程序,与串行收集器一样,单垃圾收集线程标记整理。 从上可知该收集器需要2+的CPU时才会优于串行收集器。 如下图:,并发收集器(Concurrent Low Pause Collector-CMS) -XX:+UseConcMarkSweepGC:这是以上两种策略的升级版。 年轻代:同样是暂停应用程序,多个垃圾收集线程并行的复制收集。 年老代:则只有两次短暂停,其他时间应用程序与收集线程并发的清除。对响应时间要求高的服
8、务器应用,应手工配置使用这种收集器。(重要)-XX:CMSInitiatingOccupancyFraction=68%配合使用,可以适当增大此值。如下图:,增量并发收集器(Incremental Concurrent-Mark-Sweep/i-CMS):在CPU资源不足下使用CMS。 即将问世的Garbage First Collection(G1):JDK7.0,新的算法与巨大的性能提升。(世界属于搞C+的,也属于搞Java的,但最终还是属于搞C的),查看堆,mqq16_186:/resin-wapqq2008 jmap -heap 28146 Attaching to process I
9、D 28146, please wait. Debugger attached successfully. Server compiler detected. JVM version is 11.2-b01using parallel threads in the new generation. using thread-local object allocation. Concurrent Mark-Sweep GCHeap Configuration:MinHeapFreeRatio = 40MaxHeapFreeRatio = 70MaxHeapSize = 1048576000 (10
10、00.0MB)NewSize = 262144000 (250.0MB)MaxNewSize = 262144000 (250.0MB)OldSize = 786432000 (750.0MB)NewRatio = 7SurvivorRatio = 8PermSize = 21757952 (20.75MB)MaxPermSize = 88080384 (84.0MB),Heap Usage: New Generation (Eden + 1 Survivor Space):capacity = 235929600 (225.0MB)used = 38844960 (37.0454406738
11、28125MB)free = 197084640 (187.95455932617188MB)16.464640299479168% used Eden Space:capacity = 209715200 (200.0MB)used = 37723976 (35.97638702392578MB)free = 171991224 (164.02361297607422MB)17.98819351196289% used From Space:capacity = 26214400 (25.0MB)used = 1120984 (1.0690536499023438MB)free = 2509
12、3416 (23.930946350097656MB)4.276214599609375% used To Space:capacity = 26214400 (25.0MB)used = 0 (0.0MB)free = 26214400 (25.0MB)0.0% used concurrent mark-sweep generation:capacity = 786432000 (750.0MB)used = 43648992 (41.626922607421875MB)free = 742783008 (708.3730773925781MB)5.55025634765625% used
13、Perm Generation:capacity = 21757952 (20.75MB)used = 8137176 (7.760215759277344MB)free = 13620776 (12.989784240722656MB)37.39863016519202% used,查看线程,状态 调用堆栈 辅助阅读代码 垃圾收集线程 观察线程池大小,qq16_186:/resin-wapqq2008 jstack 28146 | more 2009-09-01 11:31:50 Full thread dump Java HotSpot(TM) 64-Bit Server VM (11.2
14、-b01 mixed mode):“Attach Listener“ daemon prio=10 tid=0x00002aaaf5b08400 nid=0x49db runnable 0x00000000000000000x0000000000000000java.lang.Thread.State: RUNNABLE“NioProcessor-3“ prio=10 tid=0x00002aaaf44bb800 nid=0x7a86 runnable 0x0000000042b530000x0000000042b53ca0java.lang.Thread.State: RUNNABLEat
15、sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:215)at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:65)at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:69)- locked (a sun.nio.ch.Util$1)- locked (a java.util.
16、Collections$UnmodifiableSet)- locked (a sun.nio.ch.EPollSelectorImpl)at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:80)at org.apache.mina.transport.socket.nio.NioProcessor.select(NioProcessor.java:68)at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProce
17、ssor.java:883)at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)at java.lang.Thread.run(Thread.java:
18、619),“pool-8-thread-12“ prio=10 tid=0x00002aaaf4197400 nid=0x6f25 waiting on condition 0x000000004264e0000x000000004264eda0java.lang.Thread.State: WAITING (parking)at sun.misc.Unsafe.park(Native Method)- parking to wait for (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)at
19、java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1925)at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:358)at java.util.concurrent.ThreadPoolExecuto
20、r.getTask(ThreadPoolExecutor.java:947)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)at java.lang.Thread.run(Thread.java:619)“Gang worker#0 (Parallel GC Threads)“ prio=10 tid=0x0000000040118c00 nid=0x6df4 runnable “Gang worker#1 (Parallel GC Threads)“ prio=10 tid=0
21、x000000004011a400 nid=0x6df5 runnable “Gang worker#2 (Parallel GC Threads)“ prio=10 tid=0x000000004011c000 nid=0x6df6 runnable “Gang worker#3 (Parallel GC Threads)“ prio=10 tid=0x000000004011d800 nid=0x6df7 runnable “Concurrent Mark-Sweep GC Thread“ prio=10 tid=0x00002aaaf263b000 nid=0x6df8 runnable
22、,看对象数量,mqq16_186:/resin-wapqq2008 jmap -histo 28146 | headnum #instances #bytes class name -1: 51463 53202176 B2: 46612 39488368 C3: 7853 10870384 I4: 256 4207192 Ljava.util.concurrent.ConcurrentHashMap$HashEntry;5: 51340 2875040 java.util.HashMap$KeyIterator6: 12896 1594832 7: 12896 1554848 mqq16_1
23、86:/resin-wapqq2008 jmap -histo 28146 | tail459: 1 16 java.util.Hashtable$EmptyIterator460: 1 16 sun.reflect.GeneratedSerializationConstructorAccessor1461: 1 16 java.lang.ApplicationShutdownHooks462: 1 16 org.apache.mina.util.DefaultExceptionMonitor463: 1 16 java.lang.String$CaseInsensitiveComparato
24、r464: 1 16 java.util.ResourceBundle$Control465: 1 16 sun.util.resources.LocaleData$LocaleDataResourceBundleControl466: 1 16 .URLClassLoader$7467: 1 16 java.lang.reflect.ReflectAccess Total 433176 132451240,参数调谐,一个Java进程的示例: 28146 /usr/local/jdk/bin/java -Xss1m -XX:ErrorFile=/usr/local/app/GeneralSes
25、sionServer/log/jvm_error.log -Xmx1000m -Xms1000m -XX:NewSize=250m -XX:MaxNewSize=250m -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=80 -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCApplicationStoppedTime -cp com.qq.gss.server.GeneralSessionSer
26、ver 看看控制台输出: Total time for which application threads were stopped: 0.0292230 seconds Total time for which application threads were stopped: 0.0004750 seconds Total time for which application threads were stopped: 0.0001030 seconds 2009-08-28T17:23:46.336+0800: 656.703: GC 656.703: ParNew: 219248K-1
27、6582K(230400K), 0.0410220 secs 235238K-32571K(998400K), 0.0411500 secs Times: us er=0.15 sys=0.01, real=0.04 secs,压力测试时很有用,并发和响应不尽如人意时 ErrorFile不太好懂 其实还应该有JMX SIM ListenerServer是简化的监控,Listener public class AdminAndMonitor Autowired private SessionServerImpl ssi;Autowired private MemoryStore memory;A
28、utowired private DiskStore disk;Command(“shutdown“) public String shutdown(String param) ssi.stop();System.exit(0);return “GeneralSessionServer stopped.“; Command(“onlineUserCount“) public String onlineUserCount(String param) return memory.getOnlineUserCount()+“; Command(“storeToDisk“) public String
29、 storeToDisk(String param) boolean ok=disk.storeToDisk();return “Online data saved to disk.|success=“+ok; Command(“loadFromDisk“) public String loadFromDisk(String param) boolean ok=disk.loadFromDisk();return “Online data loaded from disk.|success=“+ok; ,常见的OOM类型,java.lang.OutOfMemoryError: Java heap spacejava.lang.OutOfMemoryError: PermGen spacejava.lang.OutOfMemoryError: request bytes for . Out of swap space,THE END.,