Java Memory Model and GC Principle

Source: Internet
Author: User

Java Memory Model http://blog.sina.com.cn/s/blog_4e90b3ba0100m5cb.html

Sun Official Website: Sun
Java Virtual Machine Model

The JVM memory model contains two major blocks: New Generation and old generation. in new generation, there is a space called Eden, mainly used to store new objects, and there are two consumer vor spaces (from, ), they are used to store objects that survive each garbage collection. In old generation, it mainly stores memory objects with long lifecycles in applications, and permanent
Generation is mainly used to put JVM reflection objects, such as class objects and method objects.

In the new generation block, the copying algorithm is generally used for garbage collection, which is fast. During each GC, the surviving objects are first copied from Eden to a certain sequence vor space. When the sequence vor space is full, the remaining live objects are directly copied to the old generation. Therefore, after each GC, the Eden memory block is cleared. In the old generation block, the Mark-compact algorithm is generally used for garbage collection, which is slower, but reduces memory requirements.
Garbage collection is divided into multiple levels. garbage collection in the old section will be collected when the level 0 is full. garbage collection in the Level 1 or above will only be collected in the new section, memory overflow usually occurs after garbage collection of the old or perm segments, and there is still no memory space to accommodate new Java objects.

 

1, out of memoryOnly when the JVM recycles the old and perm generation methods, it cannot obtain enough memory.

When a URL is accessed, the memory application process is as follows:
A. JVM will try to initialize a memory area for the relevant Java object in Eden
B. When the Eden space is sufficient, the memory application is completed. Otherwise, go to the next step.
C. JVM tries to release all inactive objects in Eden (which belongs to 1 or more advanced garbage collection). If the Eden space is not enough to put new objects, try to put some active objects in Eden into the same vor Area
D. The primary vor area is used as the intermediate swap area of Eden and old. When the old area has sufficient space, objects in the primary vor area will be moved to the old area. Otherwise, objects in the primary vor area will be retained.
E. When there is not enough space in the old area, JVM will perform full garbage collection in the old area (Level 0)
F. after full garbage collection, if the primary VOR and old areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for the new object in the Eden area, "Out of memory error" appears"


Cause of full GC:

A lot of new objects are not automatically released-> Eden memory is not enough-> the object is constantly migrated to the Old-> the old is full-> full GC

How to prevent full GC ,:

1. cache is used

     There are two types of access: the first is access with a low cache hit rate, and the second is access with a high cache hit rate. in the first case, there is no need to cache the data. Instead, the cache is ineffective, wasting memory, improving program efficiency, and wasting space, especially if the traffic level is large, it will cause full GC. in the second case, you have to cache a lot of objects. If you do not cache the objects, you have to call the database or use Io for others. In this case, you have to try to reduce the size of the cached objects, for example, you do not need to cache unnecessary data or merge some data to reduce memory usage. if the problem persists, add the machine and memory.

      Conclusion: the smaller the cache object, the higher the hit rate, and the lower the hit rate.

2. If the cache is not used, it seems that full GC will not occur unless the memory is too small or the settings are incorrect. The program has a vulnerability.

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.