pmorch

MemoryUsage.java

Nov 2nd, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. // See http://stackoverflow.com/questions/33318688/will-openjdk-jvm-ever-give-heap-memory-back-to-linux
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class MemoryUsage {
  6.  
  7.     private static void growMemory() throws InterruptedException {
  8.         System.out.println("Allocating/growing memory");
  9.         List<Long> list = new ArrayList<>();
  10.         // Experimentally determined factor. This gives approximately 1750 MB
  11.         // memory in our installation.
  12.         long realGrowN = 166608000; //
  13.         for (int i = 0 ; i < realGrowN ; i++) {
  14.             list.add(23L);
  15.         }
  16.  
  17.         System.out.println("Memory allocated/grown - sleeping before Garbage collecting");
  18.         Thread.sleep(10*1000);
  19.     }
  20.  
  21.     public static void main(String[] args) throws InterruptedException {
  22.         System.out.println("Sleeping before allocating memory");
  23.         Thread.sleep(10*1000);
  24.  
  25.         growMemory();
  26.  
  27.         System.gc();
  28.  
  29.         System.out.println("Garbage collected - sleeping forever");
  30.         while (true) {
  31.             Thread.sleep(1*1000);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment