hello everyone!
I want to increase heap memory and i know that i have to write Xmx <memsize>m parameter , but i want to set this property from java code . does anyone know how to do this?
please someone write a little piece of code
thanks
How to set Heap size
Page 1 of 12 Replies - 17262 Views - Last Post: 30 March 2009 - 02:13 PM
Replies To: How to set Heap size
#2
Re: How to set Heap size
Posted 30 March 2009 - 01:08 PM
dato.java, on 30 Mar, 2009 - 01:50 PM, said:
but i want to set this property from java code . does anyone know how to do this?
As far as I know, you don't. Java programs run inside the JVM, you can't being inside the container and also lift it up.
Wanting to mess with the heap size at runtime raises a major red flag. If you're always getting heap errors, it's most likely the program, not the JVM.
#3
Re: How to set Heap size
Posted 30 March 2009 - 02:13 PM
You probably can't, or atleast shouldnt, change the heap size of a running instance of a program, but you can start another with a different heap setting from within it.
This is a toy example that works in windows that you might be able to play with.
I don't I recommend using exec() if don't know what your doing though, it's pretty easy to do stupid things when using it.
I actually managed to crash my computer while testing out variations of the above code!
::edited a few time for typos.
This is a toy example that works in windows that you might be able to play with.
//run this from command with: java SpawnAndChangeHeap -spawn public class SpawnAndChangeHeap { public static void main(String[] args){ //Get the jvm heap size. long heapSize = Runtime.getRuntime().totalMemory(); JOptionPane.showMessageDialog(null, "" + heapSize ); if(args.length > 0 && args[0].equals("-spawn")) { try { Process proc; proc = Runtime.getRuntime().exec("cmd.exe /c java -Xms32m -Xmx128m SpawnAndChangeHeap /n"); } catch(Exception e) {System.out.println("something went wrong"); } } System.exit(0); } }
I don't I recommend using exec() if don't know what your doing though, it's pretty easy to do stupid things when using it.
I actually managed to crash my computer while testing out variations of the above code!

::edited a few time for typos.
This post has been edited by LaFayette: 30 March 2009 - 02:30 PM
Page 1 of 1