A brief summary of Java garbage collection mechanism

Source: Internet
Author: User

1. The meaning of garbage collection
in theJava, the memory becomes garbage when no object reference points to the memory that was originally assigned to an object. JVMa system-level thread automatically frees the block of memory. Garbage collection means that the object that the program no longer needs is"Useless Information" , this information will be discarded. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is later used by the new object. In fact, garbage collection can also erase memory-logged fragments in addition to releasing useless objects . the memory is fragmented because the creation object and the garbage collector free up the memory space that the discarded objects occupy. Fragmentation is a free memory hole between the blocks of memory allocated to an object. Defragmentation moves the occupied heap memory to one end of the heap, JVMallocates the sorted memory to the new object.
garbage collection can automatically free up memory space and reduce the burden of programming. This makesJavavirtual machines have some advantages. First, it can make programming more efficient. In the absence of a garbage collection mechanism, it may take a lot of time to solve a difficult memory problem. In useJavawhen programming language, the garbage collection mechanism can greatly shorten the time. Secondly, it protects the integrity of the program,garbage Collection isJavaan important part of the language security strategy.
One potential drawback of garbage collection is that its overhead affects program performance. JavaThe virtual machine must trace the objects that are useful in the running program and eventually release the useless objects. This process takes the processor's time. Secondly, the incompleteness of garbage collection algorithm , some garbage collection algorithms used earlier can not guarantee100%collect all of the discarded memory. Of course, with the continuous improvement of garbage collection algorithm and the running efficiency of hardware and software, these problems can be solved.

2 Garbage collection has the following several characteristics :
(1the unpredictability of garbage collection: Due to the implementation of different garbage collection algorithms and the use of different collection mechanisms, it may be timed to occur, possibly when the system is idleCPUresource, it may also be the same as the original garbage collection, which occurs when memory consumption is at its limit, and is related to the choice of the garbage collector and the specific settings.
(2) The accuracy of garbage collection: mainly including2Aspects: (aThe garbage collector is able to accurately mark the living object;bThe garbage collector is able to pinpoint the referential relationships between objects. The former is a precondition for completely reclaiming all discarded objects, or it may cause a memory leak. The latter is necessary for the realization of algorithms such as merging and copying. All unreachable objects can be reliably recycled, all objects can be reassigned, objects are copied and object memory is shrunk, which effectively prevents fragmentation of memory.
(3There are now many different garbage collectors, each with its algorithm and its performance, both stopping the application when garbage collection starts, and allowing the application's thread to run when garbage collection starts, and at the same time, the garbage collection runs multithreaded.
(4) The implementation of garbage collection and specificJVMas wellJVMhas a very close relationship with the memory model. of differentJVMmay use different garbage collection, whileJVMThe memory model determines theJVMWhat types of garbage collection can be used. Now,HotSpotSeriesJVMThe memory systems in the system are designed with an advanced object-oriented framework, which makes the seriesJVMcan be used in the most advanced garbage collection.
(5with the development of technology, modern garbage collection technology offers many optional garbage collectors, and different parameters can be set when each collector is configured, which makes it possible to obtain optimal application performance according to different applications.
3 for the above characteristics, we should pay attention to when using:
(1do not attempt to assume that garbage collection takes place at any time, all of which are unknown. For example, a temporary object in a method becomes a useless object when the method call is complete, and its memory can be freed at this time.
(2)Javaprovides some classes for garbage collection and provides a way to enforce garbage collection--calledSystem.GC (), but this is also an uncertain approach. Javadoes not guarantee that garbage collection can be initiated every time the method is called, it will onlyJVMIt is an unknown question whether such an application is actually performing garbage collection.
(3Pick a garbage collector that suits you. In general, if your system does not have special and demanding performance requirements, you can useJVMthe default option. Otherwise, you might consider using a targeted garbage collector, such as an incremental collector, which is more suitable for systems with higher real-time requirements. The system has a high configuration, there are more idle resources, you can consider the use of parallel tags/clears the collector.
(4The key is also difficult to grasp the problem is the memory leak. Good programming habits and rigorous programming attitude is always the most important, do not let your own a small error caused a large memory leak.
(5release the reference to the useless object as soon as possible. Most programmers use temporary variables to make reference variables exit the active domain(Scope)is automatically set toNULL, implies that the garbage collector collects the object, and must also be aware that the referenced object is listening, and if so, remove the listener and then assign a null value.

A brief summary of Java 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.