Skip to content

Instantly share code, notes, and snippets.

@barron9
Last active September 17, 2021 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barron9/01143176a8c896dbfdf6582d14dc19a0 to your computer and use it in GitHub Desktop.
Save barron9/01143176a8c896dbfdf6582d14dc19a0 to your computer and use it in GitHub Desktop.
HowToMemoryLeakInJava
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
*/
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