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.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With