Nothing Special   »   [go: up one dir, main page]

JP 7 2 Practice Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Java Programming

7-2: Java Memory Structure


Practice Solutions

Lesson Objectives:
• Introduce Java Heap Memory
• Garbage collection
• Analyze the Memory allocation in JVM

Vocabulary:

Identify the vocabulary word for each definition below.

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)

Metaspace The memory area that contains the class metadata.

Full GC The GC that collects garbage from the Tenured space.

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

Give different size of Memory to check the activities of JVM GC.

1. Check the output from the console of the Java command. How Many GC happened during the startup of
the program?

2. Use jps to list the active Java process.

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.

- Analyze the following output: from the java console

[GC (Allocation Failure)


Desired survivor size 1048576 bytes, new threshold 7 (max 15)
[PSYoungGen: 8849K->1016K(9216K)] 11741K->9410K(19456K), 0.0281064 secs] [Times:user=0.03
sys=0.00, re-al=0.03 secs]
[Full GC (Ergonomics) [PSYoungGen: 1016K->0K(9216K)] [ParOldGen: 8394K->7305K(10240K)] 9410K-
>7305K(19456K), [Metaspace: 14327K->14327K(1062912K)], 0.0395641 secs]
Times: user=0.03 sys=0.00, real=0.04 secs]

- Analyze the data from the output of jstat

5. Print out the time of the GC happened.

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?

Notice the size of OU from 2901.9 – 7717.9

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.

You might also like