模拟OOM及DecdLock实践
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
堆内存溢出
永久代内存溢出,持久不断加载类
不能创建新的线程
请求的数组长度太大
死锁
栈内存溢出,死循环递归调用