JVM参数

来源:互联网 发布:株洲中车 知乎 编辑:程序博客网 时间:2024/06/09 20:27
Trace跟踪参数
n-verbose:gc
n-XX:+printGC
n可以打印GC的简要信息
[GC4790K->374K(15872K), 0.0001606 secs]
[GC4790K->374K(15872K), 0.0001474 secs]
[GC4790K->374K(15872K), 0.0001563 secs]
[GC4790K->374K(15872K), 0.0001682 secs]


n-XX:+PrintGCDetails
打印GC详细信息
n-XX:+PrintGCTimeStamps
打印CG发生的时间戳
n
[GC[DefNew: 4416K->0K(4928K), 0.0001897 secs] 4790K->374K(15872K), 0.0002232 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 


n-XX:+PrintGCDetails的输出
Heap
def new generation   total 13824K, used 11223K [0x27e80000,0x28d80000, 0x28d80000)
  eden space 12288K,  91% used [0x27e80000, 0x28975f20, 0x28a80000)
 from space 1536K,   0% used[0x28a80000, 0x28a80000, 0x28c00000)
 to   space 1536K,   0% used [0x28c00000, 0x28c00000, 0x28d80000)
tenured generation   total 5120K, used 0K [0x28d80000,0x29280000, 0x34680000)
  the space 5120K,   0% used[0x28d80000, 0x28d80000, 0x28d80200, 0x29280000)
compacting perm gen  total 12288K, used 142K [0x34680000,0x35280000, 0x38680000)
  the space 12288K,   1% used[0x34680000, 0x346a3a90, 0x346a3c00, 0x35280000)
   ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400,0x39080000)
   rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00,0x39c80000)


n-Xloggc:log/gc.log
指定GC log的位置,以文件输出
帮助开发人员分析问题





n-XX:+PrintHeapAtGC
每次一次GC后,都打印堆信息


{Heapbefore GC invocations=0 (full 0):

 def new generation   total 3072K, used 2752K [0x33c80000,0x33fd0000, 0x33fd0000)

  eden space 2752K, 100% used[0x33c80000, 0x33f30000, 0x33f30000)

  from space 320K,   0% used [0x33f30000, 0x33f30000, 0x33f80000)

  to  space 320K,   0% used [0x33f80000,0x33f80000, 0x33fd0000)

 tenured generation   total 6848K, used 0K [0x33fd0000,0x34680000, 0x34680000)

   the space 6848K,   0% used [0x33fd0000, 0x33fd0000, 0x33fd0200,0x34680000)

 compacting perm gen  total 12288K, used 143K [0x34680000,0x35280000, 0x38680000)

   the space 12288K,   1% used [0x34680000, 0x346a3c58, 0x346a3e00,0x35280000)

    ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400,0x39080000)

    rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00,0x39c80000)

[GC[DefNew:2752K->320K(3072K), 0.0014296 secs] 2752K->377K(9920K), 0.0014604 secs][Times: user=0.00 sys=0.00, real=0.00 secs]

Heapafter GC invocations=1 (full 0):

 def new generation   total 3072K, used 320K [0x33c80000,0x33fd0000, 0x33fd0000)

  eden space 2752K,   0% used [0x33c80000, 0x33c80000, 0x33f30000)

  from space 320K, 100% used [0x33f80000,0x33fd0000, 0x33fd0000)

  to  space 320K,   0% used [0x33f30000,0x33f30000, 0x33f80000)

 tenured generation   total 6848K, used 57K [0x33fd0000,0x34680000, 0x34680000)

   the space 6848K,   0% used [0x33fd0000, 0x33fde458, 0x33fde600,0x34680000)

 compacting perm gen  total 12288K, used 143K [0x34680000,0x35280000, 0x38680000)

   the space 12288K,   1% used [0x34680000, 0x346a3c58, 0x346a3e00,0x35280000)

    ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400,0x39080000)

    rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00,0x39c80000)

}


n-XX:+TraceClassLoading
监控类的加载
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]





堆的分配参数
n-Xmx –Xms
指定最大堆和最小堆
-Xmx20m -Xms5m 运行代码:
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");



n-Xmx20m -Xms5m  运行代码
byte[] b=new byte[1*1024*1024];
System.out.println("分配了1M空间给数组");
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");

分配了1M空间给数组

Xmx=19.375M

free mem=3.4791183471679688M

total mem=4.875M

分配了1M空间给数组Xmx=19.375Mfree mem=3.4791183471679688Mtotal mem=4.875M


n-Xmx20m -Xms5m  运行代码
b=new byte[4*1024*1024];
System.out.println("分配了4M空间给数组");
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
分配了4M空间给数组Xmx=19.375Mfree mem=3.5899810791015625Mtotal mem=9.00390625M


n-Xmx20m -Xms5m  运行代码
System.gc();
System.out.println("回收内存");
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
n
回收内存Xmx=19.375Mfree mem=6.354591369628906Mtotal mem=10.75390625M


n-Xmn
设置新生代大小
n-XX:NewRatio
新生代(eden+2*s)和老年代(不包含永久区)的比值
4 表示 新生代:老年代=1:4,即年轻代占堆的1/5
n-XX:SurvivorRatio
设置两个Survivor区和eden的比
8表示 两个Survivor :eden=2:8,即一个Survivor占年轻代的1/10






















n根据实际事情调整新生代和幸存代的大小
n官方推荐新生代占堆的3/8
n幸存代占新生代的1/10
n在OOM时,记得Dump出堆,确保可以排查现场问题


永久区分配参数
n-XX:PermSize  -XX:MaxPermSize
设置永久区的初始空间和最大空间
他们表示,一个系统可以容纳多少个类型
n





n-Xss
通常只有几百K
决定了函数调用的深度
每个线程都有独立的栈空间
局部变量、参数 分配在栈上




public class TestStackDeep {

  privatestatic int count=0;

  publicstatic void recursion(long a,longb,long c){

  longe=1,f=2,g=3,h=4,i=5,k=6,q=7,x=8,y=9,z=10;

  count++;

  recursion(a,b,c);

  }

  publicstatic void main(String args[]){

  try{

    recursion(0L,0L,0L);

  }catch(Throwablee){

  System.out.println("deep of calling = "+count);

  e.printStackTrace();

  }

  }

}

递归调用-Xss128Kdeep of calling = 701java.lang.StackOverflowError-Xss256Kdeep of calling = 1817java.lang.StackOverflowError







原创粉丝点击