Reputation: 12484
I'm inexperienced with advanced Java so please bear with me.
I'm curious about Java's ability to implement features that may be termed "autonomic". Say we have two Java programs running. And one program determines that the other one is hogging memory, and thus kills that program and/or allocates more memory to the JVM.
I know in Java you can see what the available memory is (see How to do I check CPU and Memory Usage in Java?), but what if we want to dig deeper?
Thank You.
Upvotes: 15
Views: 2464
Reputation: 12363
1. Increasing memory for JVM
You can specify the initial and the maximum heap size in Java using the following code..
java -Xms64m -Xmx256m ...
in this
-Xms<size> specifies the initial Java heap size
-Xmx<size> the maximum Java heap size.
2. Checking how much memory easy of your program uses
There is a very handly tool that is given you when you install jdk in your computer which shows how much memory, heap space all running java programs in taking in your computer.
Java Visual VM
http://docs.oracle.com/javase/6/docs/technotes/guides/visualvm/
you can find it in your installed bin folder of jdk.
A previous stackoverflow post should also help Monitoring own memory usage by Java application
Upvotes: 9
Reputation: 1144
You Asked:-is it possible to increase the JVM's available memory and/or kill other Java programs?
Yes It is Possible You can increase the heap size of jvm
like :
java -Xmx512M ClassName //512M = memory you want to increase
To Kill a Process like : taskkill /F /IM <processname>.exe
Note
->but its not a good idea because the heap may be of a fixed size or may be expanded depending on the garbage collector's policy.
->open your Command prompt type taskkill/?
to know the details.
3 . And You can check total memory , free memory and max memory currently reserved by jvm
in this way.
int MegaBytes = 1024*1024 ;
long freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;
long totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
long maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;
System.out.println("Memory used by JVM: " + (maxMemory - freeMemory));
System.out.println("freeMemory in JVM: " + freeMemory);
System.out.println("totalMemory in JVM : " + totalMemory);
System.out.println("maxMemory in JVM: " + maxMemory);
For more information please check here .
Upvotes: 11
Reputation: 1158
Regarding the “autonomic” part of your question, it is indeed possible to do process management from within Java, albeit limited:
Assuming that all processes to be managed will have always been
started from within your Java application using
ProcessBuilder.start()
, you can use the destroy()
method on the process you want to kill. See this answer for more information.
Detecting when a process has too much memory implies being able to gather information about the process. The trouble is that information can only be accessed by the operating system. Inevitably, you'll have to use some kind of command or third party tool to get it. For further info, have a look at this other answer.
Although vaguely related, shutdown hooks allow you to do some critical chores automatically when the application closes. For instance, saving/closing any document(s)/file(s) left open. You can read more in this related answer.
Hope that helps.
Upvotes: 4
Reputation: 85
You can try adding -XmsMBm when trying to run a program. eg java -Xms64m ClassName
Upvotes: 1