Skip to content

Instantly share code, notes, and snippets.

@tacksoo
Created March 28, 2014 15:17
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 tacksoo/9835213 to your computer and use it in GitHub Desktop.
Save tacksoo/9835213 to your computer and use it in GitHub Desktop.
Java examples of memory issues
public class ArrayTooBig
{
public static void main(String[] args)
{
String [] strs = new String[Integer.MAX_VALUE];
}
}
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);
}
}
}
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