Skip to content

Instantly share code, notes, and snippets.

@aghosn
Created August 21, 2019 21:29
Show Gist options
  • Save aghosn/1ddee4b0c9843c4a6c647c38ff93c4e2 to your computer and use it in GitHub Desktop.
Save aghosn/1ddee4b0c9843c4a6c647c38ff93c4e2 to your computer and use it in GitHub Desktop.
package a;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
try {
Vector<Object> v = new Vector<Object>();
while(true) {
byte b[] = new byte[1048576];
v.add(b);
}
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.out.println("We caught the exception!");
}
System.out.println("And we survived!");
}
}
/*
Java output:
~> java a.Main
java.lang.OutOfMemoryError: Java heap space
at a.Main.main(Main.java:10)
We caught the exception!
And we survived!
native-image output:
~> ./a.main
Exception in thread "main" java.lang.OutOfMemoryError: Garbage-collected heap size exceeded.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment