Skip to content

Instantly share code, notes, and snippets.

@sugitk
Created May 10, 2018 04:03
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 sugitk/ba9e0edd02692274e2bba29a712cb3d9 to your computer and use it in GitHub Desktop.
Save sugitk/ba9e0edd02692274e2bba29a712cb3d9 to your computer and use it in GitHub Desktop.
OutOfMemoryError のサンプル
import java.util.LinkedList;
import java.util.List;
// -Xmx5m -Xms1m で実行
public class OOM {
private String string;
public OOM() {
StringBuilder sb = new StringBuilder();
for (int i = 0 ; i < 10000 ; i++) {
sb.append("abcdefghijklmnopqrstuvwxyz");
}
string = sb.toString();
}
public static void main(String... args) {
try {
List<OOM> list = new LinkedList<OOM>();
while (true) {
list.add(new OOM());
}
} catch (OutOfMemoryError oome) {
System.out.println("OOM: " + oome.getMessage());
} catch (Throwable t) {
System.out.println("Other throwable: " + t.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment