Java's garbage collection mechanism

Source: Internet
Author: User

In the past, little attention to memory problems, basically no attention, this aspect of the small white, because they are writing self-entertainment small program, not pay attention to performance, not to provide services. Enterprise-level applications are significantly more demanding in terms of program robustness, so consider more questions. For large companies, in order to deal with a variety of situations, server resources are certainly sufficient, but due to a lot of applications, then we have to save as much as possible, for Java programs, memory resources are very valuable, then understand how to do garbage collection in Java is very important.

I read the reference is mainly two content [to], but have not done concrete practice, nor experience, can only talk on paper. So first of all to introduce the memory allocation in Java, Java memory is to be managed by the JVM, roughly divided into:StackAndHeapTwo sections. Stack, previously learned in the data structure, is a first into the post-out (FILO) of the structure, each element of the stack is called a frame, in Java, each has a new method is called, it will open a new space in the stack, which holds the method parameters, local variables, return address, the number of citations and so on. We know that the types of parameters and local variables can only beBasic TypeOrreferences to Objects。 The advantage of the stack is that when the called method ends, the corresponding frame inside the stack is deleted, so that the parameter local variables of the method are also emptied. The other area is a heap. What's in the heap? Yes, it is the object, because the size of the object is dynamically allocated, the stack cannot provide this functionality, and the heap can. But the problem comes with the fact that if a method ends, the object's reference is emptied, but the contents of the object itself are not emptied, but the content is more and more, and finally the program runs to a certain time, and the memory inside the heap overflows. Therefore, it is necessary to manage the memory inside the heap, and Java has provided a mechanism for garbage collection. What is garbage? What do you want to recycle? As you can see, we are going to recycle objects that have not been referenced, and these objects are called unreachable. Quite frankly, if an object is not applied, then he has no meaning. So a very simple idea, we keep a reference counter for each object, and if the counter is 0, it reclaims the object's memory. This idea is correct, but there are two problems, the first is the efficiency problem, if once the object is not referenced, the object is collected immediately, then the time efficiency will be low. Second, if two objects are applied to each other, they are actually unreachable, but not recycled. To discuss the second problem first, we define the root, that is, if we can go from the object reference in the stack and reach an object, then call it root, we recycle those objects that are not the root. And then the first question, the Java recycling mechanism is actually mixed with a variety of methods. We actually write the program, we will find a feature, object recycling, most of the new structure of the object soon, then we based on this feature, designedGenerational RecyclingMechanism. Divide the heap memory into young generations, mature generations, and permanent generations. The generational recycling mechanism uses different recycling strategies in different regions. The young generation was storing the new construction object, using copy and sweep. It is divided into 3 districts, Eden, from district, to district. Eden refers to the area where the newly constructed object has been stored since the last GC. The From zone is the last time the GC has survived for an object that is not long. to area blank. When the Eden memory is full, a GC is triggered. This will copy the From zone, the root of the Eden area to the to area (if some objects survive very long, will be copied to mature generation), and empty The original Eden, from district. If the to area is full, a portion is also copied to the mature generation. The original from area becomes the to zone, and the original to area becomes the from zone, exchanging roles. The mature generation uses mark and sweep, and when maturity is full, scans the memory once and marks the root unreachable and clears. The difference between copy and Sweep,mark and Sweep is that, once cleared, the former memory is contiguous and the latter is discontinuous. In the case of permanent generations, garbage collection is not used. At this point, the Java garbage collection mechanism is probably described, in fact, Java memory is far more than heap and stack, but the part that involves garbage collection is probably these. Then slowly understand the supplement later. 1. Http://www.cnblogs.com/vamei/archive/2013/04/28/3048353.html2. http://blog.csdn.net/turkeyzhou/article/category/751216

Java's garbage collection mechanism

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.