🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.programmer
Article #411

OutOfMemoryException when memory is available

#411
From: Mike Manning
Date: Thu, 07 Oct 1999 00:00
71 lines
2923 bytes
While researching an OutOfMemoryException, when all appearances are that
there is memory available, I discovered that several others have had
this problem.

This small piece of code, borrowed from BugParade #4209215, reflects the
problem we are encountering.


public class Gctest7 {
  static long totalMem;
  static long freeMem;
  static int pFree;
  static double pctFree;
  static int count;
  static Object[] objArray;
  static Integer max;

  public static void main(String[] args) {
    max = Integer.valueOf(args[0]);
    try {
      test();
    }
    catch(Throwable thr) {
      System.err.println("Caught " + thr.getClass().getName());
      System.err.println(thr.getMessage());
      thr.printStackTrace();
      System.err.println("FAILED: " + pFree + "% free " +
                       " total = " + totalMem +
                       " free = " + freeMem +
                       " count = " + count);
    }
  }

  static void test() {
    objArray = new Object[max.intValue()];
    count = 0;

    for (int i = 0; i < max.intValue(); i++) {
      freeMem = Runtime.getRuntime().freeMemory();
      totalMem = Runtime.getRuntime().totalMemory();
      pctFree = (double) freeMem / (double) totalMem;
      pFree = (int) (100.0 * pctFree);
      System.err.println("DATA: " + pFree + "% free " +
                         " total = " + totalMem +
                         " free = " + freeMem +
                         " count = " + count);
      objArray[i] = new byte[1];
      count++;
    }
  }
}

This has been run with the -mx4m and -ms4m options, just like in the
example.

It will consistently throw the OutOfMemory exception showing something
like 37% available.  I have tried it over several JDKs on both classic
and hotspot VMs.

I find it hard to believe that this simple program fails and Sun does
not consider it to be a bug.

I realize that upping the mx parm will help, but in the real life
example it is already quite large, and there is memory available.

Any advice or workarounds that get the above to function properly would
be greatly appreciated.

Thanks!

Mike
Technical Details
Message-ID: <37FCC92D.F923AB42@peakss.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,aa2b9e2404c5bed9,start
X-Google-Attributes: gidf74b3,public
X-Google-Thread: 10a146,aa2b9e2404c5bed9,start
X-Google-Attributes: gid10a146,public
X-Google-Thread: 10bb9b,aa2b9e2404c5bed9,start
X-Google-Attributes: gid10bb9b,public
From: Mike Manning <mmanning@peakss.com>
Subject: OutOfMemoryException when memory is available
Date: 1999/10/07
Message-ID: <37FCC92D.F923AB42@peakss.com>
X-Deja-AN: 533960970
Content-Transfer-Encoding: 7bit
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Trace: 7 Oct 1999 10:24:56 -0600, 206.168.141.4
MIME-Version: 1.0
Newsgroups: comp.lang.java.programmer,comp.lang.java.machine,borland.public.jbuilder.java.language