Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, is it possible to increase the JVM's available memory and/or kill other Java programs?

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.

like image 339
Caffeinated Avatar asked Jun 20 '13 03:06

Caffeinated


1 Answers

You Asked:-is it possible to increase the JVM's available memory and/or kill other Java programs?

  1. Yes It is Possible You can increase the heap size of jvm like :

          java -Xmx512M ClassName //512M = memory you want to increase
    
  2. 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 .

like image 199
Sam Avatar answered Oct 01 '22 19:10

Sam