OOM

-XX:+HeapDumpOnOutOfMemoryError
可以用Mat jvm的堆栈分析工具分析java_pid2316.hprof文件

一:栈溢出
1.线程请求的栈深度大于虚拟机允许的最大深度 StackOverflowError
2.虚拟机在扩展栈深度时,无法申请到足够的内存空间 OutOfMemoryError

package oom;
/**
 * 栈溢出oom是错误,不是异常
 *方法的深度递归调用
 */
public class StackOverflowErrorDemo {

	public static void main(String[] args) {
		StackOverflowError();
	}

	private static void StackOverflowError() { //java.lang.StackOverflowError
		StackOverflowError();
	}

}

栈溢出:输出

Exception in thread "main" 
java.lang.StackOverflowError 
at oom.StackOverflowErrorDemo.StackOverflowError(StackOverflowErrorDemo.java:13)

二:堆溢出
创建对象时如果没有可以分配的堆内存,就会出现堆溢出。

package oom;

import java.util.Random;

/**
 * 堆溢出
 * java.lang.OutOfMemoryError: Java heap space
 * 
 * jvm配置参数:      -Xms10m -Xmx10m
 *   -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags
 */
public class JavaHeapSpaceDemo {
	
	public static void main(String[] args) {
		try {
			String str = "jmdf";
			while(true){
				str += str + new Random().nextInt(1111)+new Random().nextInt(2222);
				str.intern();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

}

堆OOM输出:

-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
[GC (Allocation Failure) [PSYoungGen: 1813K->494K(2560K)] 1813K->901K(9728K), 0.0015017 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2203K->376K(2560K)] 2610K->1783K(9728K), 0.0011504 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 1750K->376K(2560K)] 7162K->5787K(9728K), 0.0004757 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 376K->392K(2560K)] 5787K->5803K(9728K), 0.0003451 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 392K->0K(2560K)] [ParOldGen: 5411K->3212K(7168K)] 5803K->3212K(9728K), [Metaspace: 2623K->2623K(1056768K)], 0.0047641 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 40K->32K(2560K)] 5922K->5913K(9728K), 0.0010967 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 32K->32K(1536K)] 5913K->5913K(8704K), 0.0007913 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 32K->0K(1536K)] [ParOldGen: 5881K->4547K(7168K)] 5913K->4547K(8704K), [Metaspace: 2623K->2623K(1056768K)], 0.0024621 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2048K)] 4547K->4547K(9216K), 0.0002995 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2048K)] [ParOldGen: 4547K->4532K(7168K)] 4547K->4532K(9216K), [Metaspace: 2623K->2623K(1056768K)], 0.0044455 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)
	at oom.JavaHeapSpaceDemo.main(JavaHeapSpaceDemo.java:17)
Heap
 PSYoungGen      total 2048K, used 41K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 1024K, 4% used [0x00000000ffd00000,0x00000000ffd0a490,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 7168K, used 4532K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 63% used [0x00000000ff600000,0x00000000ffa6d340,0x00000000ffd00000)
 Metaspace       used 2655K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 282K, capacity 386K, committed 512K, reserved 1048576K

三:

package oom;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

/**
 *JVM参数
 *-XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m
 *java 8及之后的版本使用Metaspace来替代永久代。
 *-XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags 
 * 
 * Metaspace是方法区在HotSpot中的实现,它与持久代最大区别在于:Metaspace并不在虚拟机内存中而是使用本地内存
 * 也即在java8中,classe metadata(the virtual machines internal presentation of java class),被存储在叫做
 * Metaspace的native memory
 * 
 * 永久代(java8后被原空间Metaspace取代了)存放了以下信息:
 * 
 * 虚拟机加载的类信息
 * 常量池
 * 静态变量
 * 即时编译后的代码
 * 
 * 模拟Metaspace空间溢出,我们不断生成类往原空间灌,类占据的空间总是会超过Metaspace指定的空间大小。
 *
 * Caused by: java.lang.OutOfMemoryError: Metaspace
 *  ***************多少次发生异常:578
 */
public class MetaspaceOOMTest {

	static class OOMTest{}
	
	public static void main(String[] args) {
		int i= 0;
		try {
			while(true){
				i++;
				Enhancer enhancer = new Enhancer();
				enhancer.setSuperclass(OOMTest.class);
				enhancer.setUseCache(false);
				enhancer.setCallback(new MethodInterceptor() 
				{
					@Override
					public Object intercept(Object o, Method arg1, Object[] arg2, MethodProxy methodProxy) throws Throwable {
						
						return methodProxy.invokeSuper(o,args);
					}
				});
				enhancer.create();
			}
		} catch (Throwable e) {
			System.out.println("***************多少次发生异常:"+i);
			e.printStackTrace();
		}
		
	}

}

-XX:CompressedClassSpaceSize=2097152 -XX:InitialHeapSize=132236864 -XX:MaxHeapSize=2115789824 -XX:MaxMetaspaceSize=10485760 -XX:MetaspaceSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
[GC (Allocation Failure) [DefNew: 34944K->1957K(39296K), 0.0036981 secs] 34944K->1957K(126720K), 0.0037479 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [DefNew: 36901K->1862K(39296K), 0.0033540 secs] 36901K->1862K(126720K), 0.0033904 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [DefNew: 36806K->2279K(39296K), 0.0034418 secs] 36806K->2279K(126720K), 0.0034745 secs] [Times: user=0.03 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [DefNew: 37223K->1322K(39296K), 0.0045799 secs] 37223K->2652K(126720K), 0.0046131 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [DefNew: 36266K->1657K(39296K), 0.0025868 secs] 37596K->2988K(126720K), 0.0026257 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Metadata GC Threshold) [Tenured: 1330K->3256K(87424K), 0.0117117 secs] 25984K->3256K(126720K), [Metaspace: 8853K->8853K(1058816K)], 0.0117680 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Last ditch collection) [Tenured: 3256K->3256K(87424K), 0.0043864 secs] 3256K->3256K(126848K), [Metaspace: 8853K->8853K(1058816K)], 0.0044098 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
[Full GC (Metadata GC Threshold) [Tenured: 3256K->3257K(87424K), 0.0040656 secs] 3949K->3257K(126848K), [Metaspace: 8853K->8853K(1058816K)], 0.0040940 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Last ditch collection) [Tenured: 3257K->1619K(87424K), 0.0046202 secs] 3257K->1619K(126848K), [Metaspace: 8853K->8853K(1058816K)], 0.0046471 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
***************多少次发生异常:578
java.lang.OutOfMemoryError: Metaspace
	at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345)
	at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:114)
	at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
	at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
	at org.springframework.cglib.proxy.Enhancer.create(Enhancer.java:305)
	at oom.MetaspaceOOMTest.main(MetaspaceOOMTest.java:51)
Heap
 def new generation   total 39424K, used 1394K [0x0000000081e00000, 0x00000000848c0000, 0x00000000abea0000)
  eden space 35072K,   3% used [0x0000000081e00000, 0x0000000081f5c978, 0x0000000084040000)
  from space 4352K,   0% used [0x0000000084040000, 0x0000000084040000, 0x0000000084480000)
  to   space 4352K,   0% used [0x0000000084480000, 0x0000000084480000, 0x00000000848c0000)
 tenured generation   total 87424K, used 1619K [0x00000000abea0000, 0x00000000b1400000, 0x0000000100000000)
   the space 87424K,   1% used [0x00000000abea0000, 0x00000000ac034f70, 0x00000000ac035000, 0x00000000b1400000)
 Metaspace       used 8884K, capacity 10134K, committed 10240K, reserved 1058816K
  class space    used 774K, capacity 841K, committed 896K, reserved 1048576K

package oom;

import java.util.ArrayList;
import java.util.List;

/**
 * jvm参数    MaxDirectMemorySize 直接内存大小
 * -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
 * GC回收时间过长时会抛出OutOfMemoryError。过长的定义是:超过98%的时间用来做GC并且回收不到2%的堆内存
 * 连续多次GC都只回收了不到2%的极端情况下才会抛出。假如不抛出GC overhead limit 错误会发生什么情况呢?
 * 那就是GC清理的这么点内存很快会再次被填满,迫使GC再次执行,这样形成恶行循环,cpu使用率一直是100%,而GC没有任何成果。
 *
 * 
 *
 */
public class GCOverheadDemo {

	public static void main(String[] args) {
		int i = 0;
		List<String> list = new ArrayList<>();
		try {
			while(true){
				list.add(String.valueOf(++i).intern());
			}
		} catch (Throwable e) {
			System.out.println("****************************i"+i);//java.lang.OutOfMemoryError: GC overhead limit exceeded
			e.printStackTrace();
			throw e;
	
		}
	}

}

java.lang.OutOfMemoryError: GC overhead limit exceeded

[GC (Allocation Failure) [PSYoungGen: 2048K->506K(2560K)] 2048K->1650K(9728K), 0.0031758 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2372K->481K(2560K)] 3516K->3289K(9728K), 0.0033904 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2529K->504K(2560K)] 5337K->4872K(9728K), 0.0033630 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2419K->504K(2560K)] 6787K->6792K(9728K), 0.0069183 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Ergonomics) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 6288K->6711K(7168K)] 6792K->6711K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0739712 secs] [Times: user=0.16 sys=0.00, real=0.07 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2048K->1422K(2560K)] [ParOldGen: 6711K->6920K(7168K)] 8759K->8342K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0526029 secs] [Times: user=0.16 sys=0.00, real=0.05 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6920K->6920K(7168K)] 8968K->8967K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0411135 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6920K->6920K(7168K)] 8968K->8968K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0361352 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6920K->6920K(7168K)] 8968K->8968K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0364961 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6921K->6921K(7168K)] 8969K->8969K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0369142 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6923K->6923K(7168K)] 8971K->8971K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0396206 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6925K->6925K(7168K)] 8973K->8973K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0371470 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6927K->6927K(7168K)] 8975K->8975K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0377715 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6928K->6928K(7168K)] 8976K->8976K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0380212 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6930K->6930K(7168K)] 8978K->8978K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0370269 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6932K->6932K(7168K)] 8980K->8980K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0381201 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6934K->6934K(7168K)] 8982K->8982K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0359717 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6935K->6935K(7168K)] 8983K->8983K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0444900 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6937K->6937K(7168K)] 8985K->8985K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0380663 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6939K->6939K(7168K)] 8987K->8987K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0380859 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6941K->6941K(7168K)] 8989K->8989K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0385244 secs] [Times: user=0.17 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6942K->6942K(7168K)] 8990K->8990K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0372436 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6944K->6944K(7168K)] 8992K->8992K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0385276 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6946K->6946K(7168K)] 8994K->8994K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0383908 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6948K->6948K(7168K)] 8996K->8996K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0380998 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6949K->6949K(7168K)] 8997K->8997K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0375122 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6951K->6939K(7168K)] 8999K->8987K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0528890 secs] [Times: user=0.19 sys=0.00, real=0.05 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6941K->6941K(7168K)] 8989K->8989K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0394529 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6943K->6943K(7168K)] 8991K->8991K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0365920 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6945K->6945K(7168K)] 8993K->8993K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0384990 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6946K->6946K(7168K)] 8994K->8994K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0387087 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6948K->6948K(7168K)] 8996K->8996K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0387411 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6950K->6950K(7168K)] 8998K->8998K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0372680 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6952K->6952K(7168K)] 9000K->9000K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0391901 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6953K->6953K(7168K)] 9001K->9001K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0390490 secs] [Times: user=0.08 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6955K->6955K(7168K)] 9003K->9003K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0359133 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6957K->6957K(7168K)] 9005K->9005K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0372467 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6959K->6959K(7168K)] 9007K->9007K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0370296 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6960K->6960K(7168K)] 9008K->9008K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0378966 secs] [Times: user=0.17 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6962K->6962K(7168K)] 9010K->9010K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0361763 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6964K->6964K(7168K)] 9012K->9012K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0389324 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6966K->6966K(7168K)] 9014K->9014K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0375944 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6967K->6967K(7168K)] 9015K->9015K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0366714 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6969K->6969K(7168K)] 9017K->9017K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0378287 secs] [Times: user=0.17 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6971K->6971K(7168K)] 9019K->9019K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0359600 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6973K->6973K(7168K)] 9021K->9021K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0368525 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6974K->6974K(7168K)] 9022K->9022K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0371426 secs] [Times: user=0.13 sys=0.02, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6976K->6976K(7168K)] 9024K->9024K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0381276 secs] [Times: user=0.08 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6978K->6978K(7168K)] 9026K->9026K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0363246 secs] [Times: user=0.09 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6980K->6980K(7168K)] 9028K->9028K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0357109 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6981K->6981K(7168K)] 9029K->9029K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0391569 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6983K->6983K(7168K)] 9031K->9031K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0378418 secs] [Times: user=0.16 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6985K->6985K(7168K)] 9033K->9033K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0358199 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6987K->6987K(7168K)] 9035K->9035K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0377790 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6988K->6988K(7168K)] 9036K->9036K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0381561 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6990K->6990K(7168K)] 9038K->9038K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0367252 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6992K->6992K(7168K)] 9040K->9040K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0372657 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6994K->6994K(7168K)] 9042K->9042K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0370280 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6995K->6995K(7168K)] 9043K->9043K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0358095 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6997K->6997K(7168K)] 9045K->9045K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0381313 secs] [Times: user=0.17 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6999K->6999K(7168K)] 9047K->9047K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0374966 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7001K->7001K(7168K)] 9049K->9049K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0362769 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7002K->7002K(7168K)] 9050K->9050K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0385065 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7004K->7004K(7168K)] 9052K->9052K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0366635 secs] [Times: user=0.09 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7006K->7006K(7168K)] 9054K->9054K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0365507 secs] [Times: user=0.16 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7008K->7008K(7168K)] 9056K->9056K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0402179 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7009K->7009K(7168K)] 9057K->9057K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0363314 secs] [Times: user=0.17 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7011K->7011K(7168K)] 9059K->9059K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0391601 secs] [Times: user=0.08 sys=0.02, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7013K->7013K(7168K)] 9061K->9061K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0383198 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7015K->7015K(7168K)] 9063K->9063K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0400641 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7016K->7016K(7168K)] 9064K->9064K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0374359 secs] [Times: user=0.19 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7018K->7018K(7168K)] 9066K->9066K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0392209 secs] [Times: user=0.08 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7020K->7020K(7168K)] 9068K->9068K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0392116 secs] [Times: user=0.17 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7022K->7022K(7168K)] 9070K->9070K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0386043 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7023K->7023K(7168K)] 9071K->9071K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0383694 secs] [Times: user=0.14 sys=0.03, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7025K->7025K(7168K)] 9073K->9073K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0383448 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7027K->7027K(7168K)] 9075K->9075K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0373274 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7029K->7029K(7168K)] 9077K->9077K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0384130 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7030K->7030K(7168K)] 9078K->9078K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0392124 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7032K->7032K(7168K)] 9080K->9080K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0397791 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7034K->7034K(7168K)] 9082K->9082K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0378449 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7036K->7036K(7168K)] 9084K->9084K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0388893 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7037K->7037K(7168K)] 9085K->9085K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0385424 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7039K->7039K(7168K)] 9087K->9087K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0392221 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7041K->7041K(7168K)] 9089K->9089K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0393292 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7043K->7043K(7168K)] 9091K->9091K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0370410 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7044K->7044K(7168K)] 9092K->9092K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0398293 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7046K->7046K(7168K)] 9094K->9094K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0386001 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7048K->7048K(7168K)] 9096K->9096K(9728K), [Metaspace: 2618K->2618K(1056768K)], 0.0386582 secs] [Times: user=0.11 sys=0.00, real=0.04 secs] 
****************************i146721
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7050K->7049K(7168K)] 9098K->9097K(9728K), [Metaspace: 2636K->2636K(1056768K)], 0.0391747 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
java.lang.OutOfMemoryError: GC overhead limit exceeded
[Full GC (Ergonomics) 	at java.lang.Integer.toString(Integer.java:401)
[PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7052K->7050K(7168K)] 9100K->9098K(9728K), [Metaspace: 2643K->2643K(1056768K)], 0.0430086 secs] [Times: user=0.14 sys=0.00, real=0.04 secs] 
	at java.lang.String.valueOf(String.java:3099)
	at oom.GCOverheadDemo.main(GCOverheadDemo.java:23)
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
[Full GC (Ergonomics) [PSYoungGen: 2047K->0K(2560K)] [ParOldGen: 7054K->532K(7168K)] 9102K->532K(9728K), [Metaspace: 2643K->2643K(1056768K)], 0.0048177 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
	at java.lang.Integer.toString(Integer.java:401)
	at java.lang.String.valueOf(String.java:3099)
	at oom.GCOverheadDemo.main(GCOverheadDemo.java:23)
Heap
 PSYoungGen      total 2560K, used 47K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 2% used [0x00000000ffd00000,0x00000000ffd0bc30,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 532K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 7% used [0x00000000ff600000,0x00000000ff685168,0x00000000ffd00000)
 Metaspace       used 2650K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 281K, capacity 386K, committed 512K, reserved 1048576K

五:java.lang.OutOfMemoryError: Direct buffer memory

package oom;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
 * 配置参数:
 *  -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
 *  
 *  故障现象:java.lang.OutOfMemoryError: Direct buffer memory
 *  
 *  导致原因:
 *   写NIO程序经常使用ByteBuffer来读取或者写入数据,这是一种基于管道(channel)与缓冲区(Buffer)的I/O方式
 *   它可以使用Native函数库直接分配堆外内存,然后通过一个存储在java堆里面的DiverctByteBuffer对象作为这块内存的引用进行操作
 *   这样能在一些场景中显著提高性能,因为避免了在java堆和Native堆之间来回复制数据。
 *   
 *   ByteBuffer.allocate(capability)第一种方式分配在jvm堆内存,属于GC管辖范围,由于需要拷贝所以速度相对比较慢
 *   
 *   ByteBuffer.allocteDirect(capability) 第二种方式是分配OS本地内存,不属于GC管辖范围,由于不需要内存拷贝所以速度比较快
 *   
 *   但如果不断分配本地堆外内存,堆内存很少使用,用jvm就不需要执行GC,DireByteBuffer 对象们就不会被回收
 *   这时候堆内存充足,但本地内存可能已经用光了,再次尝试分配本地内存就会出现outofMemoryError ,程序直接就崩溃了
 * 
 *
 * 
 *
 */
public class DirectBufferMemoryDemo {

	public static void main(String[] args) {
		System.out.println("配置的maxDirectMemory:"+(sun.misc.VM.maxDirectMemory()/(double)1024/1024)+"MB");
		try{TimeUnit.SECONDS.sleep(1);}catch (Exception e){e.printStackTrace();}
		
		ByteBuffer bb = ByteBuffer.allocateDirect(6*1024*1024);
	}

}

配置的maxDirectMemory:5.0MB
[GC (System.gc()) [PSYoungGen: 877K->504K(2560K)] 877K->616K(9728K), 0.0007848 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 112K->582K(7168K)] 616K->582K(9728K), [Metaspace: 2692K->2692K(1056768K)], 0.0039543 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
Exception in thread "main" Heap
 PSYoungGen      total 2560K, used 61K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 3% used [0x00000000ffd00000,0x00000000ffd0f750,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 7168K, used 582K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 8% used [0x00000000ff600000,0x00000000ff691ae0,0x00000000ffd00000)
 Metaspace       used 2723K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 292K, capacity 386K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:694)
	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
	at oom.DirectBufferMemoryDemo.main(DirectBufferMemoryDemo.java:34)

六:java.lang.OutOfMemoryError:unable to create new native thread

package oom;

import java.util.concurrent.TimeUnit;

/**
 * 高并发请求服务器,经常出现如下异常,java.lang.OutOfMemoryError:unable to create new native thread
 * 准确的讲该native thread 异常与对应的平台有关
 * 
 * 导致原因:
 * 1:你的应用创建了太多线程了,一个应用进程创建多个线程,超过系统承载极限
 * 2:你的服务器并不允许你的应用程序创建这么多的线程,linux系统默认允许单个进程可以创建的线程是1024个,
 * 		你的应用创建超过这个数量,就会报 java.lang.OutOfMemoryError:unable to create new native thread
 * 
 * 解决办法:
 * 1:想办法降低你的应用程序创建线程的数量,分析应用是否真的需要创建这么多线程,如果不是把线程降低到最低
 * 2:对于有的应用,确实需要创建很多线程,远超linux系统的默认1024个线程的限制,可以通过修改linux的配置,扩大linux的默认配置。
 * zs
 * java -d . UnableCreateNewThreadDemo.java
 * cat UnableCreateNewThreadDemo.java
 * 
 * java com.jmdf.jvm.UnableCreateNewThreadDemo
 * 切换root
 * root
 * ps -ef|grep java
 * 
 * kiss -s 9 18198 
 * ============================
 * root 默认无上限
 * 普通用户1024个
 * ulimit -u 查看默认线程数
 * 
 * vim /ect/security/limits.d/90-nproc.conf
 * 
 * 
 */
public class UnableCreateNewThreadDemo {

	public static void main(String[] args) {
		for (int i = 0; ; i++) {
			System.out.println("&&&&&&&&&&&&&& i"+i);
			new Thread(() -> 
			{
				try{TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);}catch (Exception e){e.printStackTrace();}
			},""+i).start();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值