Java memory test - How to consume all the memory (RAM) on a computer

Here's the source code for a Java memory eating program I've written. Its purpose is to consume all of the memory (RAM) on a PC. It tries to allocate 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 (as below) 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).

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 wrote a bunch of stuff to RAM and then read it back in to see if I could find the problem that way.

 

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.