MMODemo.java
package dingchd; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class MMODemo { StackOverFlowMMO stackmmo; DirectMMO directmmo; HeapMMO heapmmo; ArrayLimitMMO almmo; ThreadLimitMMO threadmmo; PermGenMMO pergemmmo; MMODemo() { stackmmo = new StackOverFlowMMO(); directmmo = new DirectMMO(); heapmmo = new HeapMMO(); almmo = new ArrayLimitMMO(); threadmmo = new ThreadLimitMMO(); pergemmmo = new PermGenMMO(); } public void displayDirect() { directmmo.display(); } public void displayStack() { stackmmo.display(); } public void displayHEAP() { heapmmo.display(); } public void displayArryaLimit() { almmo.display(); } public void displayThreadLimit() throws InterruptedException { threadmmo.display(); } public void displayPermGen() throws IllegalArgumentException, Exception, Throwable { pergemmmo.display(); } class DirectMMO { public void display() { List<ByteBuffer> list = new ArrayList(); int size = 1000; int count = 0; while (true) { ByteBuffer buf = ByteBuffer.allocateDirect(size); for (int i = 0; i < size; i++) { int j = 1; buf.put((byte) j); } list.add(buf); count++; System.out.println("buffer direct allocate is " + count * size); } } } class StackOverFlowMMO { int lvl; StackOverFlowMMO() { lvl = 0; } public void display() { lvl = 0; func(); } public void func() { ++lvl; System.out.println("lvl " + lvl); func(); } } class HeapMMO { public void display() { Integer a[] = new Integer[100000000]; } } class ArrayLimitMMO { public void display() { System.out.println("Integer.MAX_VALUE is " + Integer.MAX_VALUE + "\r\n"); int[] pos = new int[Integer.MAX_VALUE - 1]; } } class PermGenMMO { Helloworld loader = new Helloworld(); // List<Object> list = new ArrayList(); public void display() throws IllegalArgumentException, Exception, Throwable { int index = 0; int count = 0; while (true) { try { loader.createClass(index); loader.loadClass(index); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(System.getProperty(null)); count++; index++; if (count > 10) break; } } // 动态创建一个类 class Helloworld extends ClassLoader implements Opcodes { public void createClass(int index) throws IOException, IllegalAccessException, IllegalArgumentException, Exception, Throwable { String name = "Example" + index; // System.out.println(name); ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(V1_1, ACC_PUBLIC, name, null, "java/lang/Object", null); for (int i = 0; i < 10000; i++) { String fun = "fun" + i; mv = cw.visitMethod(ACC_PUBLIC, fun, "()V", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("hello"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } // gets the bytecode of the Example class, and loads it // dynamically byte[] code = cw.toByteArray(); File directory = new File(""); FileOutputStream fos = new FileOutputStream(name + ".class"); fos.write(code); fos.close(); // Class<?> exampleClass = loader.defineClass("Example"+index, // code, 0, code.length); // uses the dynamically generated class to print 'Helloworld' // exampleClass.getMethods()[0].invoke(null, new Object[] { null // }); } public void loadClass(int index) throws ClassNotFoundException, InstantiationException, IllegalAccessException { String name = "Example" + index; Class a = Class.forName(name, true, this.getClass() .getClassLoader()); // System.out.println("load "+name+".class"); } } } class ThreadLimitMMO { List<Thread> list = new ArrayList(); int count = 0; Long cur; Object lock = new Object(); public void display() throws InterruptedException { System.out.println("-Xss = 128m"); while (true) { Long cur = new Long(System.currentTimeMillis()); Thread t = new Thread(cur.toString()) { @Override public void run() { int index = 0; synchronized (lock) { index = count; count++; } System.out.println(Thread.currentThread().getName() + "create,index" + index); try { Thread.sleep(60000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); list.add(t); Thread.sleep(100); } } } }
MMOTest.java
package dingchd; public class MMOTest { static MMODemo mmodemo = new MMODemo(); static DeadLockDemo tdemo = new DeadLockDemo(); final static int DIRECTMMO = 0; final static int STACKMMO = 1; final static int HEAPMMO = 2; final static int PGMMO = 3; final static int THREADMMO = 4; final static int ALMMO = 5; final static int DEADLOCK = 6; public static void main(String args[]) throws Throwable { int option = -1; if (args.length == 0) { System.out.println("dingchd MMO TEST \r\n"); System.out .println("0" + "\t demo java.lang.OutOfMemoryError:Direct buffer memory"); System.out.println("1" + "\t demo java.lang.StackOverflowError"); System.out.println("2" + "\t demo java.lang.OutOfMemoryError: Java heap space"); System.out.println("3" + "\t demo java.lang.OutOfMemoryError: PermGen space"); System.out .println("4" + "\t demo java.lang.OutOfMemoryError: unable to create new native thread"); System.out .println("5" + "\t demo java.lang.OutOfMemoryError: Requested array size exceeds VM limit"); System.out.println("6" + "\t demo Thread DeadLock"); return; } try { option = Integer.parseInt(args[0]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("please input valid num"); } switch (option) { case DIRECTMMO: { mmodemo.displayDirect(); break; } case STACKMMO: { mmodemo.displayStack(); break; } case HEAPMMO: { mmodemo.displayHEAP(); break; } case ALMMO: { mmodemo.displayArryaLimit(); break; } case THREADMMO: { mmodemo.displayThreadLimit(); break; } case PGMMO: { mmodemo.displayPermGen(); break; } case DEADLOCK: { tdemo.display(); break; } default: return; } } }
测试:
直接内存溢出,比如GZip**Stream 没有close
堆内存溢出
永久代内存溢出,持久不断加载类
不能创建新的线程
请求的数组长度太大
死锁
栈内存溢出,死循环递归调用
相关推荐
教你分析9种OOM常见原因及解决方案.docx
OOM分析工具-MemoryAnalyzer JVM dump分析利器
图片oom,解决方法 图片oom,完美deom
SurfaceView加载动画 解决OOM问题 多少帧都没问题 不会卡顿 不会卡顿
本Demo包含2个.JAVA文件,一个图片OOM处理工具类,一个MainACtivity类,通过一个Button打开手机所以相册,然后选择图片经过OOM处理,将处理后的图片显示到ImageView上。 【简单易懂,含详细注释(开发版本:安卓4.4.2...
android解决OOM
加载大图片到内存时如何防止 OOM的产生
java jvm 中关于内存溢出分享,举例说明各种情况下可能会出现的oom事故
基本上解决了OOM问题 如果 方便可以直接引用BitmapManager类到 项目中使用 解决blog 地址http://www.cnblogs.com/liongname/articles/2345087.html
用于复现 OOM bug,模拟JVM调优经历-JVMTest
OOM全称”Out Of Memory”,即内存溢出。 内存溢出已经是软件开发历史上存在了近40年的“老大难”问题。在操作系统上运行各种软件时,软件所需申请的内存远远超出了物理内存所承受的大小,就叫内存溢出。 内存溢出...
JVM状态监控与OOM案例分析…… 简单认识,了解
事件驱动解析是把文件转换成xml,然后一边读取一边解析,这样就对内存的占用就会很少,可以很好的处理poi出现OOM的问题。 maven添加需要的jar包 <groupId>org.apache.poi <artifactId>poi <version>3.15 ...
介绍了android开发中OOM错误的原因。
用LruCache解决OOM 并且可以下载多个相同地址的图片
问题原因分析:使用ScriptEngine.eval每次都会对脚本进行编译,生成一个新的类,被GroovyClassLoader加载,大量执行计算后,将导致被加载的类数量不断增加,最终OOM。 解决办法:对计算的表达式expression进行预...
安卓图片压缩类,避免内存溢出OOM。 利用安卓自身的计算方法。
把图片缓存、手势及OOM三个主题放在一起,是因为在Android应用开发过程中,这三个问题经常是联系在一起的。首先,预览大图需要支持手势缩放,旋转,平移等操作;其次,图片在本地需要进行缓存,避免频繁访问网络;...
android 永远不会oom的瀑布流
安卓 OOM内存