Brad
Brad

Reputation: 4547

Using -Xmx memory parameter may cause error "Could not create the Java virtual machine"

I have created a java application and many users use it. I have bundled the application in an exe file, where it is being run using this command :

java -jar -Xms256m -Xmx1024m MyApplication.jar

I am using this high "-Xmx" value because sometimes the application may need high memory to work or it may cause "Out of memory" error.

The application runs just fine for me, as i have 3GB Rams, but many other users get the error "Could not create the Java virtual machine" at startup because they have low memory: 1.5GB, or 2GB.

I am stuck here between the 2 errors "Out of memory" and "Could not create the Java virtual machine" !!

Is there a java parameter to reserve 1024 memory if this memory is available, and if not then reserve the maximum memory it can when needed ?

Upvotes: 3

Views: 7838

Answers (3)

RP-
RP-

Reputation: 5837

The Java Virtual Machine wants a contiguous allocation of memory in windows. If the RAM is 2GB and it may not have enough contiguous block of memory of 1GB, hence the error.

Upvotes: 1

matt freake
matt freake

Reputation: 5090

I don't believe there is.

The bigger problem you face is that, if you get Out Of Memory when you set the Xmx lower (i.e the same Xmx available to other user) then they will also get Out of Memory , so you haven't really helped anything by allowing them to set a lower Xmx.

I think the simple answer is that you need to set some minimum memory requirements for running your program.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533520

Either you need 1 GB or you don't.

If you would like to use more memory if its available, I would try using the -server JVM which defaults to a maximum of 1/4 of main memory instead. It can also make long running programs faster. ;)

In your case it will make the maximum heap (not total memory) to be

1.5 GB -> 0.38 GB
2 GB   -> 0.5 GB
3.5 GB -> 0.88 GB

This may be close enough. You might also consider using off heap memory as this can give you more options in terms of how you use your memory.

BTW: 4 GB of memory can cost less than $20. :P

Upvotes: 3

Related Questions