Here’s the source code for a Java “memory eating” program I wrote. Its purpose is to consume all of the memory (RAM) on a PC by allocating 1 MB byte arrays until it runs out of RAM:
import java.util.Vector;
public class MemoryEater
{
public static void main(String[] args)
{
Vector v = new Vector();
while (true)
{
byte b[] = new byte[1048576];
v.add(b);
Runtime rt = Runtime.getRuntime();
System.out.println( "free memory: " + rt.freeMemory() );
}
}
}
It’s also important to run this Java memory eating program with the -Xmx
option to make sure your JVM gets all the memory you want it to have:
$ java -Xmx1024M MemoryEater
The example shown above will try to consume up to 1,024 MB RAM (1 GB). As I write about in this How to control Java heap size (memory) allocation (xmx, xms) post, you can also specify the Xmx
parameter in gigabytes, such as this setting for one gigabyte:
-Xmx1g or -Xmx1G
I wrote a program similar to this when I started getting the Windows blue screen of death several times. I figured there was some type of hardware failure going on, so I tried to consume all the RAM in the system and then read it back in to see if the hardware failure was related to the RAM.