Skip to main content

How to avoid java.lang.OutOfMemoryError

5 replies [Last post]
acrbb
Offline
Joined: 2010-03-31
Points: 0

Hi All,
I have a program which will consume huge memory and OOM sometimes. To avoid, I try to get the size of JVM heap size, if required than available, than exit. Below is exmaple.

But the free memory number is not very correct. Is there any way to get the accurate number of JVM heap size to deal with my case?

Runtime runtime = Runtime.getRuntime();
long freeMem=runtime.freeMemory();
System.out.println(freeMem);
int[] a=new int[(int)freeMem*9+1];
System.out.println("Over...");

Thanks in advance.
Allen

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
carloscs
Offline
Joined: 2003-08-11
Points: 0

> Runtime runtime = Runtime.getRuntime();
> long freeMem=runtime.freeMemory();

This will always be a guesstimate and can always fail...

1. Run the program with more memory - 1GB for example : java -Xmx1024m ...

2. 32bit vms can only have somewhat less than 2GB memory (depends on os and native libraries) : run with a 64bit vm and more memory 8GB: java -Xmx8g ...

peter__lawrey
Offline
Joined: 2005-11-01
Points: 0

Your application will perform very badly if you try to use 100% of available memory.
You need to have a clear idea of what you are trying to achieve. "free" memory has many interperations.
I would worry more about what your code is doing, why are you trying to create an array which is 36 times the free memory? How did you chose this number?

acrbb
Offline
Joined: 2010-03-31
Points: 0

Hi Peter,

Thanks for you replay. What I try to do is avoiding out of memory issue?

I have a project which draw a big picture to the web page. If the picture is huge, out of memory happed. So I try to do some calculation before allocating memory for paint. If the memory is enough, draw the picture, If not, just give up.

My question is, how can i do it? The code I provide is just for exmplain my problem. Sorry for the confuse.

Thanks.
Allen

peter__lawrey
Offline
Joined: 2005-11-01
Points: 0

One way to approxiate the available memory is to trigger a GC and then take the available memory. This doesn't work so well if your program is doing anything else as other components can be using up memory while you do this.

I am not sure what problem fyou are really trying to solve here. You can catch OutOfMemoryErrors and continue processing. It a bit of work, but to me is sounds simpler to perform you work and only fail if you get an OutOfMemoryError.

- if you could get the available memory, is won't be accurate if anything else is running while you do your processing.
- estiating exactly how much memory you will need for a task is difficult for even simple tasks.

acrbb
Offline
Joined: 2010-03-31
Points: 0

Hi Peter,

Here is my code.

int width=9000;
int height=9000;
BufferedImage ssi = new BufferedImage(width,height,java.awt.image.BufferedImage.TYPE_INT_ARGB);

As it is a java.lang.OutOfMemoryError: Java heap space is a error and can't be catched.
[b]As width and height will be calcaulated by other compoent. It may be big.I am trying to do something to avoid crash and OOM[/b]

Do you have any ideas?

Thanks.
Allen