JVM虚拟机学习(虚拟机参数)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiao__jia__jia/article/details/85044290

                                  JVM虚拟机学习(虚拟机参数)

  1. java虚拟机概述和基本概念
  2. 堆、栈、方法区
  3. 了解虚拟机参数

java虚拟机的原理



java虚拟机的基本结构



基本概念说明




堆、栈、方法区概念和联系



Java堆





java栈



Java方法区



虚拟机参数


堆分配参数

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:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
max memory:20316160
free memory:5277824
total memory:6094848
[GC (Allocation Failure) [DefNew: 797K->192K(1856K), 0.0174730 secs] 797K->533K(5952K), 0.0269581 secs] [Times: user=0.00 sys=0.00, real=0.03 secs] 
分配了1M
max memory:20316160
free memory:4465584
total memory:6094848
[GC (Allocation Failure) [DefNew: 1249K->0K(1856K), 0.0298828 secs][Tenured: 1557K->1557K(4096K), 0.0036534 secs] 1591K->1557K(5952K), [Metaspace: 2668K->2668K(1056768K)], 0.0432009 secs] [Times: user=0.00 sys=0.00, real=0.04 secs] 
分配了4M
max memory:20316160
free memory:4534352
total memory:10358784
Heap
 def new generation   total 1920K, used 69K [0x00000000fec00000, 0x00000000fee10000, 0x00000000ff2a0000)
  eden space 1728K,   3% used [0x00000000fec00000, 0x00000000fec11420, 0x00000000fedb0000)
  from space 192K,   0% used [0x00000000fedb0000, 0x00000000fedb0000, 0x00000000fede0000)
  to   space 192K,   0% used [0x00000000fede0000, 0x00000000fede0000, 0x00000000fee10000)
 tenured generation   total 8196K, used 5653K [0x00000000ff2a0000, 0x00000000ffaa1000, 0x0000000100000000)
   the space 8196K,  68% used [0x00000000ff2a0000, 0x00000000ff825740, 0x00000000ff825800, 0x00000000ffaa1000)
 Metaspace       used 2675K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K

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];
		}
	}
}


堆溢出的处理
 

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]);
		}
		
	}
}

运行后把生成的dump文件放置在eclipse项目里,用Eclipse Memory Analyzer.进行分析。(不常用)


栈配置

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();
		}
	}
}

方法区


直接内存配置


Client和Server虚拟机工作模式(jdk1.7以后不再区分这两种模式)



 

猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/85044290