Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 727
Brad Avatar asked Jul 23 '26 08:07

Brad


2 Answers

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

like image 100
Peter Lawrey Avatar answered Jul 24 '26 22:07

Peter Lawrey


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.

like image 24
matt freake Avatar answered Jul 24 '26 23:07

matt freake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!