Out of memory error

Discussion in 'Java' started by mdtorre@freemail.it, Apr 28, 2005.

  1. Guest

    My program throws an OutOfMemory error and I don't know what to do.
    The application dose a cycle (every second) reading the outputs of an
    instrument. After a day the program stops in OutOfMemoryError blocking
    the computer.
    If I print the total memory every cycle, it starts with a heap of
    7,806,976 bytes passing after a short time to 9,695,232 bytes until it
    halts due to the error. The free memory floats between 1,600,000 to
    2,300,000 bytes.

    The strange thing is that if I execute the following simple "memory
    eating" program

    public class HeapTest {
    public static void main(String[] args) {
    Runtime rt = Runtime.getRuntime();
    java.util.Vector v = new java.util.Vector();
    while (true) {
    long size = rt.freeMemory();
    System.out.println("Total memory = " + rt.totalMemory()

    + ", free memory = " + size);
    byte[] buffer = new byte[(int) size];
    v.addElement(buffer);
    }
    }
    }

    the output is:

    Total memory = 2031616, free memory = 1881280
    Total memory = 3915776, free memory = 1936728
    Total memory = 5853184, free memory = 1937392
    Total memory = 7118848, free memory = 1265648
    Total memory = 10543104, free memory = 3424392
    Total memory = 12849152, free memory = 2306032
    Total memory = 18952192, free memory = 6103024
    Total memory = 23121920, free memory = 4169712
    Total memory = 34013184, free memory = 10891248
    Total memory = 41488384, free memory = 7475184
    Total memory = 61018112, free memory = 19529712
    Total memory = 66650112, free memory = 5547984
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    This means that the jvm can allocate up to 66,650,112 bytes, a lot more
    that
    the limit recahed in my application.

    What could be the cause of the Out of Memory?

    Thank you,
    Matteo
     
    , Apr 28, 2005
    #1
    1. Advertising

  2. Thomas Weidenfeller, Apr 28, 2005
    #2
    1. Advertising

  3. Guest

    Could you suggest me some shareware or freeware profiler?

    thank you.
     
    , Apr 28, 2005
    #3
  4. You can get a trial of JProbe or JProfiler.

    I've not used the heap tools with the JProbe suite, but JProfiler has a
    very very nice interface. You can filter down your view of the heap to
    a certain class, package, or set of packages, and it will tell you how
    many objects of type X are on the stack as well as show you deltas in
    memory allocation.
     
    Collin VanDyck, Apr 28, 2005
    #4
  5. . Guest

    On Thu, 28 Apr 2005 wrote:

    > My program throws an OutOfMemory error and I don't know what to do.
    > The application dose a cycle (every second) reading the outputs of an
    > instrument. After a day the program stops in OutOfMemoryError blocking
    > the computer.
    > If I print the total memory every cycle, it starts with a heap of
    > 7,806,976 bytes passing after a short time to 9,695,232 bytes until it
    > halts due to the error. The free memory floats between 1,600,000 to
    > 2,300,000 bytes.
    >
    > The strange thing is that if I execute the following simple "memory
    > eating" program
    >
    > public class HeapTest {
    > public static void main(String[] args) {
    > Runtime rt = Runtime.getRuntime();
    > java.util.Vector v = new java.util.Vector();
    > while (true) {
    > long size = rt.freeMemory();
    > System.out.println("Total memory = " + rt.totalMemory()
    >
    > + ", free memory = " + size);
    > byte[] buffer = new byte[(int) size];
    > v.addElement(buffer);
    > }
    > }
    > }
    >
    > the output is:
    >
    > Total memory = 2031616, free memory = 1881280
    > Total memory = 3915776, free memory = 1936728
    > Total memory = 5853184, free memory = 1937392
    > Total memory = 7118848, free memory = 1265648
    > Total memory = 10543104, free memory = 3424392
    > Total memory = 12849152, free memory = 2306032
    > Total memory = 18952192, free memory = 6103024
    > Total memory = 23121920, free memory = 4169712
    > Total memory = 34013184, free memory = 10891248
    > Total memory = 41488384, free memory = 7475184
    > Total memory = 61018112, free memory = 19529712
    > Total memory = 66650112, free memory = 5547984
    > Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    >
    > This means that the jvm can allocate up to 66,650,112 bytes, a lot more
    > that
    > the limit recahed in my application.


    You can change the heap size for any given instance of the JVM. It could
    be that the way your application is being launched restricts the JVM heap.
    Look for -Xmx settings on the java command.

    Another possibility is that something is allocating one massive amount of
    memory. So instead of linear growth, like your test app, it is growing
    expontentially.

    > What could be the cause of the Out of Memory?


    Depending on how well you know your app, you could do a code inspection
    and figure out what is holding on to so much memory or you can get a tool
    to do it for you. I'd use something like JProbe but this is available to
    me. You might not want to go out and buy it.

    If you are just going to scan the code by hand then look for anything that
    holds onto references. Like your example above, are you pushing things
    into a vector, collection, list, etc. and never removing them?

    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
     
    ., Apr 28, 2005
    #5
  6. Robert Guest

    You realize that from the code above you're never releasing that mem
    right? You just keep adding more and more to the vector.
    OutOfMemoryException is the only logical solution here. You usually do
    something with the data and then release it or write it to a file or a
    DB. Something. You don't just keep loading data into memory. The heap
    idea above may answer your question better.
     
    Robert, Apr 28, 2005
    #6
  7. Guest

    Robert wrote:
    > You realize that from the code above you're never releasing that mem
    > right? You just keep adding more and more to the vector.
    > OutOfMemoryException is the only logical solution here. You usually

    do
    > something with the data and then release it or write it to a file or

    a
    > DB. Something. You don't just keep loading data into memory. The

    heap
    > idea above may answer your question better.


    I put the code above, that is not my program, only to show that the jvm
    can allocate up to 66,650,112 bytes but in my application the jvm goes
    in out of memory error when are allocated only 9,695,232 bytes. So I
    don't understand the reason of it going out of memory.
     
    , Apr 29, 2005
    #7
  8. wrote:

    > Robert wrote:
    >
    >>You realize that from the code above you're never releasing that mem
    >>right? You just keep adding more and more to the vector.
    >>OutOfMemoryException is the only logical solution here. You usually

    >
    > do
    >
    >>something with the data and then release it or write it to a file or

    >
    > a
    >
    >>DB. Something. You don't just keep loading data into memory. The

    >
    > heap
    >
    >>idea above may answer your question better.

    >
    >
    > I put the code above, that is not my program, only to show that the jvm
    > can allocate up to 66,650,112 bytes but in my application the jvm goes
    > in out of memory error when are allocated only 9,695,232 bytes. So I
    > don't understand the reason of it going out of memory.
    >


    Here are a couple of idea:

    1. The failing program attempts a single allocation:

    double[] d = new double[10000000];

    2. They are running with different memory limit settings.

    What does Runtime.getRuntime().maxMemory() return in each case?

    Patricia
     
    Patricia Shanahan, Apr 29, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page