jvm區(qū)域總體分兩類,heap區(qū)和非heap區(qū)。heap區(qū)又分:Eden Space(伊甸園)、Survivor Space(幸存者區(qū))、Tenured Gen(老年代-養(yǎng)老區(qū))。 非heap區(qū)又分:Code Cache(代碼緩存區(qū))、Perm Gen(永久代)、Jvm Stack(java虛擬機(jī)棧)、Local Method Statck(本地方法棧)。
堆空間是java進(jìn)程的重要組成部分,幾乎所有與應(yīng)用有關(guān)的內(nèi)存空間都和堆有關(guān)。本節(jié)主要介紹與堆有關(guān)的參數(shù)。
一.最大堆和初始堆的設(shè)置:
當(dāng)java進(jìn)程啟動(dòng)時(shí),虛擬機(jī)會(huì)分配一塊初始堆空間,可以使用參數(shù)-Xms指定這塊空間的大小。一般來說為了節(jié)省空間,虛擬機(jī)會(huì)盡可能的在初始(最?。┒芽臻g的范圍內(nèi)運(yùn)行。但是如果對(duì)空間耗盡了,虛擬機(jī)會(huì)對(duì)堆空間進(jìn)行擴(kuò)展,其擴(kuò)展上限為最大堆空間,最大堆空間可以使用參數(shù)-Xmx指定。
下面的例子,說明最大堆,初始堆以及系統(tǒng)可用內(nèi)存的含義和彼此之間的關(guān)系。堆的設(shè)置為:
-Xmx20m -Xms5m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseSerialGC
public class HeapAlloc {
public static void main(String[] args) {
System.out.print("maxMemory=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"m");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024+"m");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"m");
byte[] b=new byte[1*1024*1024];
System.out.println("分配了1M空間給數(shù)組");
System.out.print("maxMemory=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"m");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024+"m");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"m");
byte[] c=new byte[4*1024*1024];
System.out.println("分配了4M空間給數(shù)組");
System.out.print("maxMemory=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"m");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024+"m");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"m");
System.gc();
System.out.println("gc釋放空間,空閑空間增多");
System.out.print("maxMemory=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"m");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024+"m");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"m");
}
}
1. 沒有分配對(duì)象時(shí)的堆情況打?。?br>
最大堆空間:maxMemory=18m
空閑堆空間: free mem=4m
使用的堆空間:total mem=5m
使用的堆空間=空閑堆空間+對(duì)象占據(jù)的內(nèi)存
2. 分配了1M空間給數(shù)組:
最大堆空間:maxMemory=18m
空閑堆空間: free mem=3m
使用的堆空間:total mem=5m //最小堆空間能夠滿足使用
3. 分配了4M空間給數(shù)組:
最大堆空間:maxMemory=18m
空閑堆空間: free mem=2m
使用的堆空間:total mem=10m// 最小堆空間不能夠滿足使用,做了擴(kuò)容
4. gc釋放空間,空閑空間增多:
最大堆空間:maxMemory=18m
空閑堆空間: free mem=4m// gc釋放了空間
使用的堆空間:total mem=10m
-Xmx和-Xms應(yīng)該保持一個(gè)什么關(guān)系,可以讓系統(tǒng)的性能盡可能的好呢?
可以直接將初始堆-Xms和最大堆-Xmx設(shè)置相等。這樣的好處是可以減少程序運(yùn)行時(shí)進(jìn)行的垃圾回收次數(shù),從而提高系統(tǒng)的性能,但是內(nèi)存的使用空間提升了,會(huì)浪費(fèi)內(nèi)存。
二.新生代的配置:
1. 參數(shù)-Xmn: 可以設(shè)置新生代的絕對(duì)大小。設(shè)置一個(gè)較大的新生代會(huì)減少老年的的大小,,這個(gè)參數(shù)對(duì)系統(tǒng)性能以及GC行為有很多的影響。新生代的大小一般設(shè)置為這個(gè)堆的1/3或1/4左右
2. -XX:NewRatio: 新生代(eden+2*s)和老年代(不包含永久區(qū))的比值。如-XX:NewRatio: 4 表示 新生代:老年代=1:4,即年輕代占堆的1/5
3. -XX:SurvivorRatio=eden/from=eden/to :設(shè)置兩個(gè)Survivor(from/to)區(qū)和eden的比
8表示 兩個(gè)Eden:Survivor=2:8,即一個(gè)Survivor占年輕代的1/10
有兩個(gè)Survivor區(qū),一個(gè)是from區(qū),另一個(gè)是to區(qū)。
下面一個(gè)例子說明:
public class NewSizeDemo {
public static void main(String[] args) {
byte[] b=null;
for(int i=0;i<10;i++){
// b=new byte[1*1024*1024];
try {
}catch (Exception ex){
}
}
}
}
0.-Xmx20m -Xms20m -Xmn1m -XX:+PrintGCDetails:
如下沒有實(shí)例化對(duì)象時(shí),仍然有GC,說明回收的不是程序代碼實(shí)例化的對(duì)象。
[GC (Allocation Failure) [PSYoungGen: 503K->432K(1024K)] 503K->432K(19968K), 0.0008239 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 944K->496K(1024K)] 944K->496K(19968K), 0.0009576 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 1024K, used 819K [0x00000007bfe80000, 0x00000007c0000000, 0x00000007c0000000)
eden space 512K, 63% used [0x00000007bfe80000,0x00000007bfed0fb8,0x00000007bff00000)
from space 512K, 96% used [0x00000007bff80000,0x00000007bfffc010,0x00000007c0000000)
to space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
ParOldGen total 18944K, used 0K [0x00000007bec00000, 0x00000007bfe80000, 0x00000007bfe80000)
object space 18944K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bfe80000)
Metaspace used 2932K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 322K, capacity 388K, committed 512K, reserved 1048576K
用不同的堆參數(shù)執(zhí)行這段程序,虛擬機(jī)的行為表現(xiàn)受到堆空間分配的影響,
public class NewSizeDemo {
public static void main(String[] args) {
byte[] b=null;
for(int i=0;i<10;i++){
b=new byte[1*1024*1024];
try {
// Thread.sleep(1000);
}catch (Exception ex){
}
}
}
}
1. -Xmx20m -Xms20m -Xmn1m -XX:+PrintGCDetails:
- 沒有因?qū)嵗瘜?duì)象引起的GC發(fā)生。
- 全部分配在老年代。(注意加重的文字,表明10240K全部分配在老年代)。eden區(qū)無法容納任何一個(gè)1MB數(shù)組,這個(gè)偏小的新生代無法為1MB數(shù)組預(yù)留空間,故所有的數(shù)組都分配在老年代
如下GC日志:
[GC (Allocation Failure) [PSYoungGen: 503K->384K(1024K)] 503K->384K(19968K), 0.0010692 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 896K->496K(1024K)] 896K->496K(19968K), 0.0007767 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 1024K, used 945K [0x00000007bfe80000, 0x00000007c0000000, 0x00000007c0000000)
eden space 512K, 87% used [0x00000007bfe80000,0x00000007bfef04c8,0x00000007bff00000)
from space 512K, 96% used [0x00000007bff80000,0x00000007bfffc010,0x00000007c0000000)
to space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
ParOldGen total 18944K, used 10240K [0x00000007bec00000, 0x00000007bfe80000, 0x00000007bfe80000)
object space 18944K, 54% used [0x00000007bec00000,0x00000007bf6000a0,0x00000007bfe80000)
Metaspace used 3083K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 339K, capacity 388K, committed 512K, reserved 1048576K
2. -Xmx20m -Xms20m -Xmn15m -XX:+PrintGCDetails:
- 沒有因?qū)嵗瘜?duì)象引起的GC發(fā)生。
- 全部分配年輕代的eden區(qū)。(注意加重的文字,表明10240K全部分配在eden區(qū))。
- 沒有分配在老年區(qū)。
解釋:
Heap
PSYoungGen total 13824K, used 11972K [0x00000007bf100000, 0x00000007c0000000, 0x00000007c0000000)
eden space 12288K, 97% used [0x00000007bf100000,0x00000007bfcb1198,0x00000007bfd00000)
from space 1536K, 0% used [0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000)
to space 1536K, 0% used [0x00000007bfd00000,0x00000007bfd00000,0x00000007bfe80000)
ParOldGen total 5120K, used 0K [0x00000007bec00000, 0x00000007bf100000, 0x00000007bf100000)
object space 5120K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf100000)
Metaspace used 3075K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 338K, capacity 388K, committed 512K, reserved 1048576K
3. -Xmx20m -Xms20m -Xmn7m -XX:+PrintGCDetails:
- 因?qū)嵗瘜?duì)象引起的年輕代的GC發(fā)生兩次。
- 年輕代使用1606K,其中eden區(qū)使用1105。from區(qū)使用476左右。
- 年老代使用2072K。
因?yàn)閒rom(512K分配不了一個(gè)1MB的數(shù)組)和to的空間太小,已經(jīng)無法正常回收了,所以一部分對(duì)象會(huì)分配在老年代里。
[GC (Allocation Failure) [PSYoungGen: 5604K->496K(6656K)] 5604K->1564K(19968K), 0.0011327 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 5797K->480K(6656K)] 6865K->2588K(19968K), 0.0009761 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 6656K, used 1606K [0x00000007bf900000, 0x00000007c0000000, 0x00000007c0000000)
eden space 6144K, 18% used [0x00000007bf900000,0x00000007bfa19b30,0x00000007bff00000)
from space 512K, 93% used [0x00000007bff80000,0x00000007bfff8020,0x00000007c0000000)
to space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
ParOldGen total 13312K, used 2108K [0x00000007bec00000, 0x00000007bf900000, 0x00000007bf900000)
object space 13312K, 15% used [0x00000007bec00000,0x00000007bee0f030,0x00000007bf900000)
Metaspace used 2981K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 330K, capacity 388K, committed 512K, reserved 1048576K
4. -Xmx20m -Xms20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails:
- -XX:SurvivorRatio的默認(rèn)值為8
- 3次gc
- 增加了from,to區(qū)
- 由于增大了form/to區(qū),這樣對(duì)象會(huì)在年輕代回收,不會(huì)到老年代
- 老年代沒有分配,40k的使用是系統(tǒng)級(jí)別的占用。
[GC (Allocation Failure) [PSYoungGen: 3441K->1520K(5632K)] 3441K->1552K(18944K), 0.0012058 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4712K->1504K(5632K)] 4744K->1544K(18944K), 0.0010894 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4673K->1424K(5632K)] 4713K->1464K(18944K), 0.0006599 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 5632K, used 3614K [0x00000007bf900000, 0x00000007c0000000, 0x00000007c0000000)
eden space 4096K, 53% used [0x00000007bf900000,0x00000007bfb23b10,0x00000007bfd00000)
from space 1536K, 92% used [0x00000007bfd00000,0x00000007bfe64020,0x00000007bfe80000)
to space 1536K, 0% used [0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000)
ParOldGen total 13312K, used 40K [0x00000007bec00000, 0x00000007bf900000, 0x00000007bf900000)
object space 13312K, 0% used [0x00000007bec00000,0x00000007bec0a000,0x00000007bf900000)
Metaspace used 2959K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 330K, capacity 388K, committed 512K, reserved 1048576K
5. -Xmx20m -Xms20m -XX:NewRatio=1 -XX:SurvivorRatio=1 -XX:+PrintGCDetails:
*NewRatio=老年代/新生代
- 3次gc。
- 進(jìn)一步擴(kuò)大from/to區(qū)
- 都在新生代,老年代沒有對(duì)象分配
[GC (Allocation Failure) [PSYoungGen: 4500K->1600K(7680K)] 4500K->1608K(17920K), 0.0014530 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 5847K->1552K(7680K)] 5855K->1560K(17920K), 0.0011139 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 7680K, used 4903K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
eden space 5120K, 65% used [0x00000007bf600000,0x00000007bf945d60,0x00000007bfb00000)
from space 2560K, 60% used [0x00000007bfd80000,0x00000007bff04020,0x00000007c0000000)
to space 2560K, 0% used [0x00000007bfb00000,0x00000007bfb00000,0x00000007bfd80000)
ParOldGen total 10240K, used 8K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
object space 10240K, 0% used [0x00000007bec00000,0x00000007bec02000,0x00000007bf600000)
Metaspace used 3008K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 331K, capacity 388K, committed 512K, reserved 1048576K
總的來說,gc頻繁對(duì)系統(tǒng)是不好的。
為了減少gc,我們調(diào)小from/to區(qū)的空間,如下:
6. -Xmx20m -Xms20m -XX:NewRatio=1 -XX:SurvivorRatio=3 -XX:+PrintGCDetails:
- 2次gc。
- 都在新生代,老年代沒有對(duì)象分配
[GC (Allocation Failure) [PSYoungGen: 5604K->1616K(8192K)] 5604K->1624K(18432K), 0.0012332 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 6919K->1568K(8192K)] 6927K->1576K(18432K), 0.0022475 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 8192K, used 2817K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
eden space 6144K, 20% used [0x00000007bf600000,0x00000007bf7386f0,0x00000007bfc00000)
from space 2048K, 76% used [0x00000007bfe00000,0x00000007bff88010,0x00000007c0000000)
to space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
ParOldGen total 10240K, used 8K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
object space 10240K, 0% used [0x00000007bec00000,0x00000007bec02000,0x00000007bf600000)
Metaspace used 2964K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 330K, capacity 388K, committed 512K, reserved 1048576K

三.堆溢出處理:
在java程序運(yùn)行過程中,如果堆空間不足,則有可能拋出內(nèi)存溢出的錯(cuò)誤(Out Of Memory),簡(jiǎn)稱OOM。一旦發(fā)生這類問題,系統(tǒng)會(huì)被迫退出。如果發(fā)生在生產(chǎn)環(huán)境,可能引起業(yè)務(wù)的中斷。為了在發(fā)生錯(cuò)誤時(shí),獲得盡可能多的現(xiàn)場(chǎng)信息,java虛擬機(jī)提供了參數(shù)-XX:+HeapDumpOnOutOfMemoryError,使用該參數(shù),可以在內(nèi)存溢出時(shí)導(dǎo)出整個(gè)堆信息。和它配合使用-XX:+HeapDumpPath,可以指定導(dǎo)出堆的存放路徑。
用這個(gè)參數(shù)執(zhí)行程序代碼:
-Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/a.dump
public class DumpOOM {
public static void main(String[] args) {
List<byte[]> list=new ArrayList<byte[]>();
for(int i=0;i<25;i++){
list.add(new byte[1*1024*1024]);
}
}
}
輸出結(jié)果:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at jvmbasicstructure.DumpOOM.main(DumpOOM.java:19)
四.堆的分配參數(shù) – 總結(jié):
- 根據(jù)實(shí)際事情調(diào)整新生代和from/to 區(qū)的大小
- 官方推薦新生代占堆的3/8
- from區(qū)占新生代的1/10
- 在OOM時(shí),記得Dump出堆,確??梢耘挪楝F(xiàn)場(chǎng)問題。
- eden/(s0 or s1)默認(rèn)是8。