Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // See http://stackoverflow.com/questions/33318688/will-openjdk-jvm-ever-give-heap-memory-back-to-linux
- import java.util.ArrayList;
- import java.util.List;
- public class MemoryUsage {
- private static void growMemory() throws InterruptedException {
- System.out.println("Allocating/growing memory");
- List<Long> list = new ArrayList<>();
- // Experimentally determined factor. This gives approximately 1750 MB
- // memory in our installation.
- long realGrowN = 166608000; //
- for (int i = 0 ; i < realGrowN ; i++) {
- list.add(23L);
- }
- System.out.println("Memory allocated/grown - sleeping before Garbage collecting");
- Thread.sleep(10*1000);
- }
- public static void main(String[] args) throws InterruptedException {
- System.out.println("Sleeping before allocating memory");
- Thread.sleep(10*1000);
- growMemory();
- System.gc();
- System.out.println("Garbage collected - sleeping forever");
- while (true) {
- Thread.sleep(1*1000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment