title: How to keep allocating memory until 3GB have been allocated in Java?
body: <p>I really hope that my title is correct, but let me explain exactly what I mean by the title.
  I need to write a program that creates an <code>byte Array</code> with the size of 1MB and add it in an <code>ArrayList&lt;byte[]&gt;</code>.</p>
  
  <p>I need to be doing that until I have added the amount of 3GB. Also after every allocation I should use <code>Thread.sleep(20)</code>.</p>
  
  <p>I think I understand how the implementation should look like, but I'm having difficulties with calculating that exact 1MB and the variables.</p>
  
  <p>Here is how my code looks like: </p>
  
  <pre><code>import java.util.ArrayList;
  
  public class MemLeak
  {
  
      public static void main(String[] args)
      {
          int maxSize = 3221225472; // 3GB in bytes
          int startSize = 0;
  
          byte[] byteArray = new byte[1048576]; // 1MB in bytes
          ArrayList&lt;byte[]&gt; list = new ArrayList&lt;byte[]&gt;();
  
          while (startSize != maxSize) {
              list.add(byteArray);
              Thread.sleep(20);
              startSize = startSize + 1048576;
  
          }
  
      }
  }
  </code></pre>
  
  <p>In my case I have thought that the number of elements in an byte Array equals to size in bytes, but that doesn't seem to be the case.
  The Variable int <code>maxSize = 3221225472</code> is also out of int range. </p>
  
  <p>Could someone give me some pointers?
  Thanks, much appreciated!</p>