2 Replies - 7635 Views - Last Post: 30 March 2009 - 02:13 PM Rate Topic: -----

#1 Guest_dato.java*


Reputation:

How to set Heap size

Post icon  Posted 30 March 2009 - 12:50 PM

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
Is This A Good Question/Topic? 0

Replies To: How to set Heap size

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: How to set Heap size

Posted 30 March 2009 - 01:08 PM

View Postdato.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.
Was This Post Helpful? 0
  • +
  • -

#3 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

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.
//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! :P

::edited a few time for typos.

This post has been edited by LaFayette: 30 March 2009 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1