Created
May 10, 2018 04:03
-
-
Save sugitk/ba9e0edd02692274e2bba29a712cb3d9 to your computer and use it in GitHub Desktop.
OutOfMemoryError のサンプル
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.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