eclipse中jvm内存配置java
-Xmx512m -Xms512m -Xmn128m -Xss128m -XX:MaxNewSize=256m -XX:MaxPermSize=256m
OOMdom
package com.shi.jvm; public class StackTest { /** * 递归调用 */ public static void add() { add(); } /** * 栈满了异常 * Exception in thread "main" java.lang.StackOverflowError * @param args */ public static void main(String[] args) { add(); } }
package com.shi.jvm; import java.util.Random; /** * -Xms1024m -Xmx1024m -XX:+PrintGCDetails 堆初始化内存,堆最大内存 打印出堆GC回收信息 * @author shiye * */ public class HeapSpaceTest { public static void main(String[] args) { /** * 输出: MAX_MEMERY = 1808269312(字节)1724.5MB TOTAL_MEMERY = 122683392(字节)117.0MB * @param args */ // long maxMemory = Runtime.getRuntime().maxMemory();//返回Java虚拟机试图使用的最大内存量 // long totalMemory = Runtime.getRuntime().totalMemory();//返回当前使用的java虚拟机的内存总量 // System.out.println("MAX_MEMERY = " + maxMemory +"(字节)" + (maxMemory/(double)1024/1024) + "MB"); // System.out.println("TOTAL_MEMERY = " + totalMemory +"(字节)" + (totalMemory/(double)1024/1024) + "MB"); //-Xms8m -Xmx8m -XX:+PrintGCDetails String str = "www.shiye"; while(true) { str += str + new Random().nextInt(9999); } /** * 结果: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space */ } // -Xms8m -Xmx8m -XX:+PrintGCDetails // 打印垃圾回收信息 //-Xms8m -Xmx32m -XX:+HeapDumpOnOutOfMemoryError //生成内存快照文件 查看错误信息 }
package com.shi.jvm; import java.util.ArrayList; import java.util.List; /** * OOM : GC overhead limit exceeded * * * -Xmx10m -Xms10m -XX:+PrintGCDetails * @author shiye 结果: Exception in thread "main" [Full GC (Ergonomics) java.lang.OutOfMemoryError: GC overhead limit exceeded [PSYoungGen: 2047K->0K(2560K)] [ParOldGen: 7051K->517K(7168K)] 9099K->517K(9728K), [Metaspace: 2624K->2624K(1056768K)], 0.0057765 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] at java.lang.Integer.toString(Unknown Source) at java.lang.String.valueOf(Unknown Source) at com.shi.jvm.GcOverHeadTest.main(GcOverHeadTest.java:27) */ public class GcOverHeadTest { public static void main(String[] args) { List<String> list = new ArrayList<>(); int i = 0; while (true) { // a4.intern();//将该对象AA的引用保存到常量池上 list.add(String.valueOf(i++).intern()); } } }
package com.shi.jvm; import java.nio.ByteBuffer; /** * 运行参数:-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m * 直接内存溢出 * allocateDirect 使用的是直接内存,不是堆内存 * @author shiye * 結果: 配置的maDirectMemeory: 5.0MB [GC (System.gc()) [PSYoungGen: 828K->504K(2560K)] 828K->592K(9728K), 0.0037700 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (System.gc()) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 88K->566K(7168K)] 592K->566K(9728K), [Metaspace: 2647K->2647K(1056768K)], 0.0045972 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory at java.nio.Bits.reserveMemory(Unknown Source) at java.nio.DirectByteBuffer.<init>(Unknown Source) at java.nio.ByteBuffer.allocateDirect(Unknown Source) at com.shi.jvm.DirectBufferMemoryTest.main(DirectBufferMemoryTest.java:16) */ public class DirectBufferMemoryTest { public static void main(String[] args) throws InterruptedException { System.out.println("配置的maDirectMemeory: " + (sun.misc.VM.maxDirectMemory()) / (double) 1024 / 1024 + "MB"); Thread.sleep(3000); ByteBuffer b = ByteBuffer.allocateDirect(6*1024*1024);//分配6m的内存 } }
package com.shi.jvm; import java.util.concurrent.atomic.AtomicInteger; /** * OOM: 不能去建立本地线程的异常 * * @author shiye * * 改异常会在Liunx中报错:unable create native thread 异常, * */ public class UnableCreateNativeThread { public static void main(String[] args) { AtomicInteger index = new AtomicInteger(1); while (true) { new Thread(() -> { try { System.out.println("建立了线程:" + index.getAndIncrement()); Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } } }