user1321824
user1321824

Reputation: 445

Java Increase Heap size

I have a java application form which i am accessing records from database,processing it and again updaing the database. I am doing this by converting my java application to JAR and executing it from command prompt.During testing there are only 20 records per table in database.For that i am executing the jar as follows to avoid out of memory error,

java -jar -Xmx512m MyApp.jar

If database contains much record(50,000),how to increase the heap size while executing jar or How to increase the heap size dynamically based on needs while executing the jar.

Thanks

Upvotes: 2

Views: 3171

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533492

The maximum is the maximum heap you would need ever not how much it will actually use. It's the point at which you would rather than application crash than keep running. The JVM will dynamically manage the memory used up to this point.

How to increase the heap size dynamically based on needs while executing the jar.

So if you want the JVM to use 256 MB, but you want it to dynamically grow up to 30 GB you would set it like this

java -ms256m -mx30g -jar MyApp.jar

Even the minimum is not guaranteed to be used. The JVM will do minimal effort to keep the memory usage up to this level. A Hello World program will not use 256 MB of heap no matter what you set the minimum to.

Upvotes: 0

Rahul
Rahul

Reputation: 16335

This will increase your max heapSize to 1Gb.

java -Xms256m -Xmx1024m -jar MyApp.jar

where -Xms specifies the initial Java heap size and -Xmx the maximum Java heap size.

Parameters to the JVM must be passed before the -jar part. The reason for that can be seen when we execute "java -version" :

Usage:

java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

Upvotes: 0

pushy
pushy

Reputation: 9635

The maximal heap size is set before running the program by using the -Xmx option you already used. Based on this information, the JVM will use as much heap size as it needs, up to the given amount. So if your program only needs 1 MB of RAM, the JVM will only use that amount, if it needs 1G and you only gave it 512M, you will get an OutOfMemoryError.

So, basically, you can give your program more heap space if you need to, and the JVM will dynamically adjust the used heap size according to its needs.

Sometimes this is not the intended behavior, and the lower bound for heap size can be set with -Xms. Then the JVM will always reserve a heap of the size between Xms and Xmx.

Upvotes: 1

Rohit Kumar
Rohit Kumar

Reputation: 1028

-Xmx2048m -XX:-UseGCOverheadLimit

Set this in your VM Arguement. You can set less memory size in place of 2048

Upvotes: 0

Related Questions