Java garbage collection mechanism

Source: Internet
Author: User

What is garbage in a Java program? What is this rubbish and how is it recycled? Why is it recycled? What happens when you don't recycle? These are the questions we want to solve in this blog!
As we all know, in C, as programmers, you have to consider how to reclaim the allocated memory, often the problem is, when to recycle garbage, when to release? Release too early, those still need to use the object resources disappeared, facing the system of the crash, but once forgotten to reclaim useless memory, will lead to memory leaks, reduce the performance of the system. That's a big question, right? That's annoying, isn't it? But in Java programming, there is no need for the programmer to do an explicit memory recovery, because the JRE has helped us do these things, from the initial memory allocation to those not referenced objects of the collection, it is a control, it is very cool. The JRE assigns its own small membership-a small thread for detection and control, a small thread that keeps track of the health of all objects while the program is running, especially those that have been allocated for memory, but not the objects that are referenced, It will be flexible and witty to place it in the data area pointed to in the heap memory to NULL, when the CPU is idle or out of memory, a garbage collection. Memory occupies the space it recycles, and more humane is that fragmentation occurs because the object created and the garbage collector frees the memory space of the discarded resource. These fragments are those that are not allocated space, "intimidated, reasonably well" awkward situation, the JVM will collect and organize the fragments, the occupied heap of memory from one end of the heap, moving to the other end, the JVM organized memory allocated to new objects.
A brief introduction to the mechanism of garbage collection, then what are the characteristics of it? Reclaim the space of unused objects, which are the memory space in the virtual machine heap memory, it can only reclaim memory resources, for peripherals, database resources, IO resources, it does not play its own role; garbage collection or to specific circumstances, specific instructions under the different virtual machines, The garbage collection algorithm they use is not necessarily the same, so the process of recycling can be from a certain difference, some may be timed to recover, some may also be in the CPU when idle, or even when the CPU is running at the limit, garbage recycling.
In addition to this, when we write Java programs, do not refer to objects that are not needed, if you want to keep references to these objects, the garbage collection mechanism will not reclaim these objects for the time being, they will feel that you may use it at the next moment, so it will be temporarily reserved for you, so The system has less memory available, so that the garbage collection mechanism is called more frequently and the performance of the system degrades.
In fact, there are several common algorithms for garbage collection.
The first, reference counting method, is the only method of garbage collection that does not use the root set, which uses reference counters to differentiate between surviving objects and objects that are no longer in use. In general, each object in the heap corresponds to a reference counter. When an object is created and assigned to a variable each time, the reference counter is set to 1. When an object is assigned to any variable, the reference counter is incremented by 1 each time the object is scoped (the object is discarded), the reference counter is reduced by 1, and once the reference counter is 0, the object satisfies the garbage collection condition.
The second, marking and clearing the garbage collector, leads to the concept of roots, starting from the root set to scan, to identify those objects can reach and which objects are unreachable, for unreachable objects, garbage collection.
The third, compacting algorithm
To solve the heap fragmentation problem, in the process of purging, the algorithm moves all objects to one end of the heap, and the other end of the heap becomes an adjacent free memory area, and the collector updates all references to all objects it moves so that the references can recognize the original object in the new location. In the implementation of the collector based on the compacting algorithm, the handle and the handle table are generally added.
Here simply introduced three kinds of specific recovery algorithm, recovery algorithm also with the development of computer and constantly improve and improve.
In specific programming, we can also ask the JVM to invoke the method System.GC (). Request garbage collection, but as I said, just a request, when the JVM receives the message, it also takes a weighting based on the garbage collection algorithm, making the garbage collection algorithm easy to happen or prone to occur, Does not mean that after the call, garbage collection will be done directly. For the Finalize () method, the method is the method to be executed when the space is actually reclaimed, and the method first "erases" and then really frees up space.

Java garbage collection mechanism

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.