jvm基础知识1
Posted luzhouxiaoshuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jvm基础知识1相关的知识,希望对你有一定的参考价值。
堆放实例对象,栈放实例对象的引用,方法区存储创建类的信息
上面堆和垃圾回收的关系,垃圾回收回收的是堆内存的数据,s0和s1区域,
例如现在我们要清除s0中的堆对象,将s0中正在运行的对象从s0区域移动到s1区域中,然后将s0中要被清除的堆对象清除,现在对象都存储在s1中,s0中为空,即下来要清除s1中的对象,将不被清除的对象从s1中移动到s0中,将s1中的对象清除,依次类推
接下来学习下虚拟机的参数:
1、第一点给堆设置参数
-Xms5m -Xmx20m
初始化堆大小5m,最大设置为20M,-XX:+PrintCommandLineFlags将配置的参数从控制台打印输出
配置参数:
我们来看下面代码的运行结果:
package com.bjsxt.base001; public class Test01 { public static void main(String[] args) { //-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags //查看GC信息 System.out.println("max memory:" + Runtime.getRuntime().maxMemory()); System.out.println("free memory:" + Runtime.getRuntime().freeMemory()); System.out.println("total memory:" + Runtime.getRuntime().totalMemory()); byte[] b1 = new byte[1*1024*1024]; System.out.println("分配了1M"); System.out.println("max memory:" + Runtime.getRuntime().maxMemory()); System.out.println("free memory:" + Runtime.getRuntime().freeMemory()); System.out.println("total memory:" + Runtime.getRuntime().totalMemory()); byte[] b2 = new byte[4*1024*1024]; System.out.println("分配了4M"); System.out.println("max memory:" + Runtime.getRuntime().maxMemory()); System.out.println("free memory:" + Runtime.getRuntime().freeMemory()); System.out.println("total memory:" + Runtime.getRuntime().totalMemory()); } }
-XX:InitialHeapSize=5242880 -XX:MaxHeapSize=20971520 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC
max memory:20316160
free memory:4418752
total memory:5111808
[GC[DefNew: 676K->127K(1536K), 0.0014042 secs] 676K->481K(4992K), 0.0014266 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
分配了1M
max memory:20316160
free memory:3541280
total memory:5111808
[GC[DefNew: 1180K->0K(1536K), 0.0010578 secs][Tenured: 1505K->1505K(3456K), 0.0022471 secs] 1533K->1505K(4992K), [Perm : 2511K->2511K(21248K)], 0.0033312 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
分配了4M
max memory:20316160
free memory:3625944
total memory:9441280
Heap
def new generation total 1664K, used 108K [0x00000000f9a00000, 0x00000000f9bc0000, 0x00000000fa0a0000)
eden space 1536K, 7% used [0x00000000f9a00000, 0x00000000f9a1b100, 0x00000000f9b80000)
from space 128K, 0% used [0x00000000f9b80000, 0x00000000f9b80000, 0x00000000f9ba0000)
to space 128K, 0% used [0x00000000f9ba0000, 0x00000000f9ba0000, 0x00000000f9bc0000)
tenured generation total 7556K, used 5601K [0x00000000fa0a0000, 0x00000000fa801000, 0x00000000fae00000)
the space 7556K, 74% used [0x00000000fa0a0000, 0x00000000fa618670, 0x00000000fa618800, 0x00000000fa801000)
compacting perm gen total 21248K, used 2520K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb076370, 0x00000000fb076400, 0x00000000fc2c0000)
No shared spaces configured.
[GC[DefNew: 1180K->0K(1536K), 0.0010578 secs][Tenured: 1505K->1505K(3456K), 0.0022471 secs] 1533K->1505K(4992K), [Perm : 2511K->2511K(21248K)], 0.0033312 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
表示进行了垃圾回收,DefNew表示回收了新生代,Tenured表示回收了老年代,Perm 表示回收了永久区
新生代会频繁被GC不稳定,新生代包含eden空间 s1 和s0空间
我们来看下面的列子:
package com.bjsxt.base001; public class Test02 { public static void main(String[] args) { //第一次配置 //-Xms20m -Xmx20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC //第二次配置 //-Xms20m -Xmx20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC //第三次配置 //-XX:NewRatio=老年代/新生代 //-Xms20m -Xmx20m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC byte[] b = null; //连续向系统申请10MB空间 for(int i = 0 ; i <10; i ++){ b = new byte[1*1024*1024]; } } }
程序运行的效果是:
[GC[DefNew: 512K->255K(768K), 0.0013618 secs] 512K->439K(20224K), 0.0013894 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 768K, used 428K [0x00000000f9a00000, 0x00000000f9b00000, 0x00000000f9b00000)
eden space 512K, 33% used [0x00000000f9a00000, 0x00000000f9a2b300, 0x00000000f9a80000)
from space 256K, 99% used [0x00000000f9ac0000, 0x00000000f9affff8, 0x00000000f9b00000)
to space 256K, 0% used [0x00000000f9a80000, 0x00000000f9a80000, 0x00000000f9ac0000)
tenured generation total 19456K, used 10424K [0x00000000f9b00000, 0x00000000fae00000, 0x00000000fae00000)
the space 19456K, 53% used [0x00000000f9b00000, 0x00000000fa52e040, 0x00000000fa52e200, 0x00000000fae00000)
compacting perm gen total 21248K, used 2517K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb075440, 0x00000000fb075600, 0x00000000fc2c0000)
No shared spaces configured.
eden space 512K,from space 256K是 比例是2,SurvivorRatio=2 就是这个含义设置的意思 xmn表示新生代的值设置成1M,from区域就是s0区域,to区域就是s1区域,二者的大小是一样的
eden space 512K, 33% used [0x00000000f9a00000, 0x00000000f9a2b300, 0x00000000f9a80000)
from space 256K, 99% used [0x00000000f9ac0000, 0x00000000f9affff8, 0x00000000f9b00000)
to space 256K, 0% used [0x00000000f9a80000, 0x00000000f9a80000, 0x00000000f9ac0000)
上面三者之和就是1M
真正在应用的场景中,已经尽量减少GC,将初始化的内存大小和最大内存大小配置成一样,这样可以减少GC
package com.bjsxt.base001; import java.util.Vector; public class Test03 { public static void main(String[] args) { //-Xms1m -Xmx1m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/Test03.dump //堆内存溢出 Vector v = new Vector(); for(int i=0; i < 5; i ++){ v.add(new Byte[1*1024*1024]); } } }
上面这个代码我申请5M的内存,但是在设置的时候却是
/-Xms1m -Xmx1m 设置成1M,就会出现堆内存异常,使用-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath就会将信息导出来查看
package com.bjsxt.base001; public class Test04 { //-Xss1m //-Xss5m //栈调用深度 private static int count; public static void recursion(){ count++; recursion(); } public static void main(String[] args){ try { recursion(); } catch (Throwable t) { System.out.println("调用最大深入:" + count); t.printStackTrace(); } } }
运行结果:
调用最大深入:12560
java.lang.StackOverflowError
以上是关于jvm基础知识1的主要内容,如果未能解决你的问题,请参考以下文章