HowToMemoryLeakInJava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
class Custom{ | |
private String name; | |
Custom(String name){ | |
this.name = name; | |
} | |
protected void finalize(){ | |
// System.out.println("gc'd"); | |
} | |
} | |
public class Test { | |
@org.junit.Test | |
public void test() throws InterruptedException { | |
Runtime rt = Runtime.getRuntime(); | |
long freemem = rt.freeMemory(); | |
System.out.println(freemem / 1024 + " kb"); | |
for (int i = 0; i < 100; i++) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
List<Custom> list_customers= new ArrayList<>(); | |
while (true) { | |
list_customers.add( new Custom("john")); | |
} | |
} | |
}).start(); | |
} | |
while(true){ | |
Thread.sleep(5000); | |
freemem = rt.freeMemory(); | |
System.out.println(freemem/1024 +" kb"); | |
} | |
} | |
} | |
// -Xmx10m | |
/* | |
Exception in thread "Thread-11" java.lang.OutOfMemoryError: GC overhead limit exceeded | |
at Test$1.run(Test.java:27) | |
at java.lang.Thread.run(Thread.java:748) | |
Exception in thread "Thread-3" Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space | |
Exception in thread "Thread-13" java.lang.OutOfMemoryError: GC overhead limit exceeded | |
at Test$1.run(Test.java:27) | |
at java.lang.Thread.run(Thread.java:748) | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
Exception in thread "Thread-31" Exception in thread "Thread-28" java.lang.OutOfMemoryError: GC overhead limit exceeded | |
Exception in thread "Thread-33" Exception in thread "Thread-20" Exception in thread "Thread-36" Exception in thread "Thread-43" java.lang.OutOfMemoryError: GC overhead limit exceeded | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
Exception in thread "Thread-0" Exception in thread "Thread-40" Exception in thread "Thread-26" Exception in thread "Thread-39" Exception in thread "Thread-30" java.lang.OutOfMemoryError: GC overhead limit exceeded | |
Exception in thread "Thread-9" Exception in thread "Thread-44" java.lang.OutOfMemoryError: GC overhead limit exceeded | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
java.lang.OutOfMemoryError: GC overhead limit exceeded | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
class Custom { | |
String name; | |
Custom another_relation; | |
Custom(String name) { | |
this.name = name; | |
} | |
protected void finalize() { | |
// System.out.println("gc'd"); | |
} | |
} | |
public class Test { | |
@org.junit.Test | |
public void test() throws InterruptedException { | |
Runtime rt = Runtime.getRuntime(); | |
long freemem = rt.freeMemory(); | |
System.out.println(freemem / 1024 + " kb"); | |
List<Thread> t = new ArrayList<>(); | |
for (int i = 0; i < 50; i++) { | |
t.add(new Thread(new Runnable() { | |
@Override | |
public void run() { | |
List<Custom> list_customers = new ArrayList<>(); | |
while (true) { | |
try { | |
Thread.sleep(10); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
list_customers.add(new Custom("john")); | |
} | |
} | |
})); | |
t.get(i).start(); | |
} | |
while (true) { | |
Thread.sleep(5000); | |
freemem = rt.freeMemory(); | |
System.out.println(freemem / 1024 + " kb"); | |
} | |
} | |
} | |
// -Xmx10m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment