Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Out of memory error

Reply
Thread Tools

Out of memory error

 
 
mdtorre@freemail.it
Guest
Posts: n/a
 
      04-28-2005
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

 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      04-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> My program throws an OutOfMemory error and I don't know what to do.


Get a so called memory profiler and measure what eats up your memory.

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
 
 
 
mdtorre@freemail.it
Guest
Posts: n/a
 
      04-28-2005
Could you suggest me some shareware or freeware profiler?

thank you.

 
Reply With Quote
 
Collin VanDyck
Guest
Posts: n/a
 
      04-28-2005
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.

 
Reply With Quote
 
.
Guest
Posts: n/a
 
      04-28-2005
On Thu, 28 Apr 2005 (E-Mail Removed) 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

 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      04-28-2005
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.

 
Reply With Quote
 
mdtorre@freemail.it
Guest
Posts: n/a
 
      04-29-2005

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.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-29-2005
(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory card out of memory - Nikon Coolpix Club_obi_wan Digital Photography 11 3 Weeks Ago 07:20 PM
GC.Collect() not cleaning memory, how to find out what references to lots of memory still exist? DR ASP .Net 0 04-15-2008 09:03 PM
Out of memory when having lot of memory! jmr ASP .Net 5 11-23-2007 06:23 PM
How do I get an out-of-memory error memory usage dump? Todd Java 4 09-05-2007 03:08 PM
out of memory while expanding memory stream v2brothers Computer Support 1 08-29-2007 11:11 AM



Advertisments