[Question post] I would like to ask a few questions about memory allocation policies of Java virtual machines.

Source: Internet
Author: User

[Question post] I would like to ask a few questions about memory allocation policies of Java virtual machines.

I recently read Zhou Zhiming's "deep understanding of Java virtual machines", but I have a few questions that I don't quite understand. I hope my friends who have research on virtual machines can help me solve them.
First, let's talk about the environment I tested:

Operating System: Mac OS X 10.11.6 EI CapitanJava Environment: java version "1.8.0 _ 92" Java (TM) SE Runtime Environment (build 1.8.0 _ 92-b14) Java HotSpot (TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

1. I found that in the young generation, even if I was just an empty class and didn't perform any memory allocation work, the memory in the Eden area would still be occupied. The example is as follows:

public class BlankedClassSize {  public static void main(String[] args) {  // TODO Auto-generated method stub  }}

Virtual Machine parameter settings:

-verbose:gc -XX:+UseSerialGC -XX:+PrintGCDetails -Xmx20M -Xms20M -Xmn10M

The GC of the virtual machine is as follows:

Heapdef new generation  total 9216K, used1164K[0x00000007bf000000, 0x00000007bfa00000, 0x00000007bfa00000)eden space 8192K,  14% used [0x00000007bf000000, 0x00000007bf1231d0, 0x00000007bf800000)from space 1024K,   0% used [0x00000007bf800000, 0x00000007bf800000, 0x00000007bf900000)to   space 1024K,   0% used [0x00000007bf900000, 0x00000007bf900000, 0x00000007bfa00000)tenured generation   total 6144K, used 0K [0x00000007bfa00000, 0x00000007c0000000, 0x00000007c0000000)the space 6144K,   0% used [0x00000007bfa00000, 0x00000007bfa00000, 0x00000007bfa00200, 0x00000007c0000000)Metaspace       used 2666K, capacity 4486K, committed 4864K, reserved 1056768Kclass space    used 289K, capacity 386K, committed 512K, reserved 1048576K

Question 1: What occupies the memory?

2. When the young generation carries out a GC, because the young generation GC uses a replication algorithm to collect memory, why is the memory occupied by the Eden region not zero after a Minor GC operation? What is memory used? Example:

public class MinorGC {    private static final int_1MB = 1024*1024;        public static void main(String[] args) {        // TODO Auto-generated method stub        testEdenGC();    }    public static void testEdenGC(){        byte[] allocation1,allocation2,allocation3,allocation4,allocation5,allocation6;        allocation1=newbyte[_1MB];        allocation2=newbyte[_1MB];        allocation3=newbyte[_1MB];        allocation4=newbyte[_1MB];        allocation5=newbyte[_1MB];        allocation6=newbyte[_1MB];    }}

Virtual Machine parameter settings:

-verbose:gc -XX:TenuringThreshold=1 -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRation=3 -XX:+PrintGCDetails -XX:+UseSerialGC

Virtual Machine GC:

[GC (Allocation Failure) DefNew: 6029K->1354K(8192K), 0.0032985 secs] 6029K->5450K(18432K), 0.0033216 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]Heapdef new generation   total 8192K, used 2440K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)eden space 6144K,  17% used [0x00000007bec00000, 0x00000007bed0f748, 0x00000007bf200000)from space 2048K,  66% used [0x00000007bf400000, 0x00000007bf5529b0, 0x00000007bf600000)to   space 2048K,   0% used [0x00000007bf200000, 0x00000007bf200000, 0x00000007bf400000)tenured generation   total 10240K, used 4096K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)the space 10240K,  40% used [0x00000007bf600000, 0x00000007bfa00040, 0x00000007bfa00200, 0x00000007c0000000)Metaspace       used 2666K, capacity 4486K, committed 4864K, reserved 1056768Kclass space    used 289K, capacity 386K, committed 512K, reserved 1048576K

Question 2: According to 6026 K-> 1354 K (8192 K), after GC, allocation1 is transferred to region vor1, allocation2 ~ 4. Transferred to the elderly zone. At this time, the eden still has 13 54k of memory occupied. What is occupied? According to 6029 K-> 5450 K (18432 K), after GC is known, the Java heap memory is reduced by 579 K, whose memory is reduced (obviously several newly created objects are reachable and will not be recycled )?

3. when memory is allocated to new objects, a minor GC occurs because the young generation has insufficient space. The surviving objects in the young generation are copied to the old generation to free up space for new objects, according to my experiment, if the memory in the elderly area is insufficient, the young generation objects will be copied but new objects will be stored. (the guarantee will fail) the new object will be directly put into the old age, for example:

public class MinorGC {  private static  final int_1MB = 1024*1024;  public static void main(String[] args) {    // TODO Auto-generated method stub    testAllocation();  }  public static void testAllocation(){    byte[] allocation1,allocation2,allocation3,allocation4;    allocation1 = newbyte[2*_1MB];    allocation2 = newbyte[2*_1MB];    allocation3 = newbyte[2*_1MB];    allocation4 = newbyte[4*_1MB];  }}

The virtual machine parameter settings are as follows:

-verbose:gc -Xms16M -Xmx16M -Xmn10M -XX:+PrintGCDetails  -XX:SurvivorRatio=8  -XX:+HeapDumpOnOutOfMemoryError -XX:MaxTenuringThreshold=15 -XX:+UseSerialGC

GC:

[GC (Allocation Failure) [DefNew (promotion failed) : 7144K->7474K(9216K), 0.0028055 secs][Tenured: 4096K->4096K(6144K), 0.0015627 secs] 7144K->6473K(15360K), [Metaspace: 2660K->2660K(1056768K)], 0.0044048 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]Heapdef new generation   total 9216K, used 6555K [0x00000007bf000000, 0x00000007bfa00000, 0x00000007bfa00000)eden space 8192K,  80% used [0x00000007bf000000, 0x00000007bf666ed0, 0x00000007bf800000)from space 1024K,   0% used [0x00000007bf900000, 0x00000007bf900000, 0x00000007bfa00000)to   space 1024K,   0% used [0x00000007bf800000, 0x00000007bf800000, 0x00000007bf900000)tenured generation   total 6144K, used 4096K[0x00000007bfa00000, 0x00000007c0000000, 0x00000007c0000000)the space 6144K,  66% used [0x00000007bfa00000, 0x00000007bfe00020, 0x00000007bfe00200, 0x00000007c0000000)Metaspace       used 2666K, capacity 4486K, committed 4864K, reserved 1056768Kclass space    used 289K, capacity 386K, committed 512K, reserved 1048576K

According to the above output, we can see that during the GC process, the young generation's 7144 K-> and 7144 K (9216 K) did not transfer the objects in the young generation, in the old age, 4096 K-> 4096 K (6144 K), tenured generation total 6144 K, and used 4096 K indicate that allocation4 was directly transferred to the old age.

Question 3: in this case, does a virtual opportunity automatically set a large object threshold ?? Put the new object directly into the old age?

The above three questions cannot be solved by Tom on this virtual machine. I hope you can help me to explain them. Thank you for your consideration.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.