user472221
user472221

Reputation: 3124

Out of Memory Error

I have written a code and I want to pass some lists with different sizes but when the size of my list goes over 1024 ,it will throw the exception below! how can i handle it?

size, running time for x

2,184073
3,98308
5,617257
9,481714379
17,55230
33,64505
65,41094
129,65120
257,102555
513,197511
1025,465897
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at OBSTclasses.MemoizedVersion.<init>(MemoizedVersion.java:33)
        at OBSTclasses.Coordinator.main(Coordinator.java:102)
Java Result: 1

and also the line that throws this exception is :

     minAverageTimeArray = new double[array.size()][array.size()];

thanks

Upvotes: 0

Views: 298

Answers (6)

Stewart Murrie
Stewart Murrie

Reputation: 1319

Yep, it's your heapspace alright. By default Java allocates 128MB to the heap on most platforms. You should consider the maximum size of list that you're willing to process, and therefore how much memory you need. Think about it this way: a variable of type double in Java is usually 8 bytes long. If your lists are 1024 items long, then your 2D array will need 8 * 1024 * 1024 bytes, or 8MB, of heap space just for the array itself. Now, if your lists double in length, you'll need four times as much heap (32MB), and if they double again (4096 items) you'll need all 128MB of your heap space! This is, of course, ignoring all the heap used by other objects created by your program.

So, you have a few answers. As others have said, the easiest is to increase the maximum heap a JVM instance will use. You should also consider reducing the amount of memory your program needs at any one time. Are there intermediate summations or averages you can compute without needing to store all of your data? Or can you eliminate the need to store your data as both a list and an array? Can you lower the precision of your data - will floats or integers be as accurate as doubles for your application?

Upvotes: 0

Amit
Amit

Reputation: 281

As coobird said never handle the Error in java. You can use MAT (Memory Analyzer - http://www.eclipse.org/mat/) to check if you really has any memory leak or its just that heap memory is less for JVM. In case of memory leak you can optimize the memory footprint using the result of MAT. Else you can increase the heap size as already mentioned above by many friends.

Upvotes: 0

Deepak
Deepak

Reputation: 2102

Possible reasons of OutOfMemory Error could be a memory Leak
Solution: Increase the heap size by using the following command

Usage :: java -Xms<initial heap size> -Xmx<maximum heap size>
Defaults are:java -Xms32m -Xmx128m
Other values might be java -Xms128m -Xmx512m
-Xms - Initial Heap Size.
-Xmx - Extended(Maximum) Heap Size. m-megabytes

Upvotes: 0

coobird
coobird

Reputation: 161022

As malfy's answer mentions, if one encounters an OutOfMemoryError, aside from finding a way to use less memory, increasing the heap space by telling the JVM to allocate more memory to the heap is one way to handle the situation.

In general, one should not perform error handling against an Error such as an OutOfMemoryError. An Error, as opposed to an Exception, is an condition thrown by the JVM which notes that a problem that is fatal to the JVM has occurred, which is something that can't be truly "handled" by the program itself.

From the Java API Specification for the Error class:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

So, to answer the question concisely, you should not be error handling the OutOfMemoryError, but find ways to avoid that Error from occurring in the first place.

Upvotes: 3

Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

It sounds like you need to increase the maximum amount of memory that your java process is allowed to use. Add a parameter like -Xmx512m to your java invocation, where 512m means 512 meegabytes.

Upvotes: 2

atx
atx

Reputation: 5079

You'll have to increase the Java VM's heap space, see here: http://hausheer.osola.com/docs/5

Upvotes: 3

Related Questions