0

I was looking at the following article: Increase heap size in Java

Now I have a program that needs about 5GB memory and while doing what was told in the article (that is increasing heap size by using -Xmx5g in the arguments field), I am still getting

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

My system is Windows 7 (64 bit) with 8GB RAM. Am I doing something wrong? If yes, how shall I proceed to get 5GB of heap memory or it is just not feasible for my system to handle?

Note: I have to do calculations with a 2D matrix that is of 25K*25K size having all non-zero values. Hence I cannot use sparse matrix as well.

Community
  • 1
  • 1
Shrikant Kakani
  • 1,511
  • 2
  • 17
  • 37

2 Answers2

2

OutOfMemoryError is thrown when JVM does not have enough memory for objects being allocated. If you defined heap of 5G this almost definitely mean that you have a kind of memory leak. For example I can write very simple code that will cause OutOfMemoryError at any environment:

List<String> list = new LinkedList<>();
while(true) {
    list.add("a");
}

Run this code and wait several seconds. OutOfMemoryError will be thrown. This is because I add strings to list and never clean it.

I believe that something similar happens with your application.

I understand, that it is not so trivial as my example, so you will probably have to use profiler to debug it and understand the reason of your memory leak.

EDIT: I've just saw that you are working with 25K*25K martrix. It means that you have 625M cells. You have not mentioned the type of the matrix but if it is int that occupies 4 bytes you need 625*4=2500M=2.5G memory, so 5G should be enough.

Please try to analyze what else happens in your program and where your memory is spent.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

5G/(25K*25K) ~ 8 bytes.

Generously assuming that you program does not use memory except for that matrix, each matrix element must take no more than 8 bytes.

You should calculate at least approximate memory requirements to check whether it is even possible to handle problem of such size on your hardware. For example, if you need a 2D array of MxN size of double values then you need at least 8*M*N bytes of memory.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
  • Well (8*M*N)b + 128m I'd say, you need breathing space for the application outside of that matrix too ;) – Gimby Mar 27 '14 at 09:47