JP 7 2 Practice Solution
JP 7 2 Practice Solution
JP 7 2 Practice Solution
Lesson Objectives:
• Introduce Java Heap Memory
• Garbage collection
• Analyze the Memory allocation in JVM
Vocabulary:
Heap A data area from which memory for all class instances and arrays is allocated
Minor GC The GC which collects garbage from the Young space (Eden and Survivor spaces)
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Try It/Solve It:
Run the Testing Program
java -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -
XX:+PrintCommandLineFlags -XX:+PrintTenuringDistribution -jar TestMemory.jar
1. Check the output from the console of the Java command. How Many GC happened during the startup of
the program?
C:\tmp>jps -l
2088 sun.tools.jps.Jps
21000 TestMemory.jar
3. Use the jstat command to check the output and find the change of heap.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
2
4. Give the value of 1M input to the application.
Use -XX:+PrintGCTimeStamps
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
3
6. Run the application sizetest.jar:
java -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -
XX:+PrintTenuringDistribution -XX:PretenureSizeThreshold=20000 -jar sizetest.jar
In this lab, the program will generate 4 MB heap memory, but the data will reside in the Tenured area, not in
the Young Eden space.
7. Check the output from the jstat utility. Where is the 4 MB heap memory located?
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.