Created
March 28, 2014 15:17
-
-
Save tacksoo/9835213 to your computer and use it in GitHub Desktop.
Java examples of memory issues
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
public class ArrayTooBig | |
{ | |
public static void main(String[] args) | |
{ | |
String [] strs = new String[Integer.MAX_VALUE]; | |
} | |
} |
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
public class HeapOutOfMemeory | |
{ | |
/** | |
* Cause a Heap OutOfMemory Error | |
* | |
* @param args | |
* @throws FileNotFoundException | |
*/ | |
public static void main(String[] args) throws FileNotFoundException | |
{ | |
ArrayList<PrintStream> al = new ArrayList<PrintStream>(); | |
Random r = new Random(); | |
File file = new File("docs/pg2600.txt"); //text of "war and peace" which is about 3MB | |
long start = System.currentTimeMillis(); | |
for(;;) { | |
PrintStream s = new PrintStream(file); | |
al.add( s ); | |
long end = System.currentTimeMillis(); | |
System.out.println(end - start); | |
} | |
} | |
} |
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
public class StackOverflow { | |
public static void main(String[] args) | |
{ | |
recur(); | |
} | |
public static void recur() { | |
recur(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment