Increase JVM memory using Java Code
Hi Friends
I am in a critical situtation and not able get the solution. My application is huge and need of increase in JVM memory. I am able to increase JVM memory in command prompt
java -Xmx500m classname
Simarly I am able to run jar file also
java -Xmx500m -jar sample.jar
I need to know how to set JVM memory for executable JAR. When I just double-click the JAR application load and i am getting Out Of Memory. I dont what ti use command prompt to execute. Please help me with this program. Thanks in advance
[569 byte] By [
DeepanNa] at [2007-11-26 13:24:12]

# 1
On Windows you can use a bat file[code]:: Launch the application without a console window::start javaw -Xmx500m -jar sample.jar[/code]On *nix it is as easy.
# 2
Thanks !! it is working !! i feel still this is only the temperary solution.
I am interested to know how to execute the jar file just by double clicking and also out of memory should not occur.
Reason i stress the above point is, I have converted jar to exe, in this case ur suggestion wont wont. I feel there should be some way to allocated JVM memory in java code itself.
If anyone have the answer for this it would be helpful for me.
Message was edited by:
DeepanN
# 3
> I have converted jar to exe. > I feel there should be some way to allocated JVM memory in java code itself.Presumably whatever utility you used to convert jar to exealso allows you to set Java heap parameters.
# 4
> Reason i stress the above point is, I have converted
> jar to exe, in this case ur suggestion wont wont. I
> feel there should be some way to allocated JVM memory
> in java code itself.
Your feeling would be wrong.
The java language and VM is set up explicitly to prevent that. It is considered a security feature.
If you want to increase the memory then
1. Write an installer to set it up that way.
2. Provide documentation to allow the user to manually set it.
3. Deliver it with OS specific code (not in java) which does that.
4. Write an OS specific wrapper (not in java) which does that.
# 5
I gone through the code of ImageJ software. ImageJ is a freeware and wrtten in Java. They are setting jvm memory in code, getting the description of the code was diffcult.
long memoryBytes = Runtime.getRuntime().maxMemory();
long memoryMB = memoryBytes/1024/1024;
long memoryDiff = memoryMB - 64;
String s = "Runtime.maxMemory: " + memoryMB + "MB\n";
s = s + "Difference: " + memoryDiff + "MB";
System.out.println("s..." + s);
The above code will give you the memory allocated for JVM. when there is a way to find the memory space, there should be some way to set it. I am still trying to set the memory in code r using external resource.
I want to just give the executable jar to users. They are not satisified with the bat file and hidding command prompt in back groud.
# 6
> I gone through the code of ImageJ software. ImageJ is
> a freeware and wrtten in Java. They are setting jvm
> memory in code, getting the description of the code
> was difficult.
>
> long memoryBytes =
> s = Runtime.getRuntime().maxMemory();
>long memoryMB = memoryBytes/1024/1024;
>long memoryDiff = memoryMB - 64;
>
> String s = "Runtime.maxMemory: " + memoryMB +
> B + "MB\n";
>s = s + "Difference: " + memoryDiff + "MB";
>System.out.println("s..." + s);
>
> The above code will give you the memory allocated for JVM.
Yes.
> When there is a way to find the memory space,
> there should be some way to set it.
No.
> I am still trying to set the memory in code r using external resource.
Read reply 4.
> I want to just give the executable jar to users. They
> are not satisified with the bat file and hidding
> command prompt in background.
You may be able to use Runtime.exec() to launch another instance of java, with whatever heap setting you desire.
# 7
can u give me a example code of how to use Runtime.exec()
# 8
Google [ [url= http://www.google.com/search?q=java+runtime.exec+tutorial]java runtime.exec tutorial[/url] ]