user1100085
user1100085

Reputation: 11

Getting java out of memory error? even though i given enough initial and max memory sizes to JVM

I have an application which will use 300 million size of hash maps. For that I initialized the JVM with initial and maximum heap size of 16 GB i.e. (java -Xms16384M -Xmx16384M) but I am getting java OutOfMemoryError. my machine is having 32 GB RAM with RedHat Linux operating system installed. I observed that JVM is using only 25% of RAM from "top" command in Linux i.e 8GB even though i set it for 16GB it is using only 8GB.

I want to know that if JVM is limiting the RAM?

How to configure JVM to use complete min and max heap size given?

Upvotes: 0

Views: 715

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533520

Can you try this program?

System.out.println("Max memory "+Runtime.getRuntime().maxMemory()/100 /1000+" MB.");
int mb = 0;
try {
    List<byte[]> bytes = new ArrayList<>();
    while (true) {
        bytes.add(new byte[1000 * 1000]);
        mb++;
    }
} catch (OutOfMemoryError expected) {
}
System.out.println("OOME after " + mb + " MB allocated");

what does it print for your settings? For -mx8g I get

Max memory 7635 MB.
OOME after 7025 MB allocated

For -mx12g I get

Max memory 11453 MB.
OOME after 10012 MB allocated

and with -mx12g -XX:MaxNewSize=512m I get

Max memory 12705 MB.
OOME after 12863 MB allocated

Upvotes: 1

Costi Ciudatu
Costi Ciudatu

Reputation: 38195

The garbage collector needs some of the heap too (almost half of it if most of your stuff is in the young generation which is handled by a copying collection); that memory will be kept untouched to be used by the GC.

Upvotes: 0

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23208

Can you post the exception stack trace? If the figures you posted are correct, you might be possibly running out of permanent memory (permagen errors) in which case you also need to add -XX:PermSize and/or -XX:MaxPermSize flags. Which JVM version are you using?

Upvotes: 0

Related Questions