Understanding Java garbage collection

When the program creates objects, arrays and other reference type entities, the system will allocate a piece of memory for this object in the heap memory, and the object will be saved in this memory. When this memory is no longer referenced by any reference variables, this memory will become garbage and wait for the garbage collection mechanism to recycle. Garbage collection mechanism has three characteristics:

The garbage collection mechanism is only responsible for recycling objects in the heap memory, It will not recycle any physical resources (such as database connection, open file resources, etc.), nor will it recycle the memory allocated for the object in a way other than the way of creating the object, (for example, the memory applied by the object calling malloc in the local method) the program cannot accurately control the operation of garbage collection. It can only recommend garbage collection. There are two recommended methods: system. GC () and runtime getRuntime(). GC () always calls its finalize () method before garbage collecting any object, but the timing is the same as that of garbage collection, and the timing of calling the finalize () method is uncertain. In view of the above three characteristics, there are three problems:

1. You must clean up manually to release the memory and other physical resources allocated in other ways than creating objects. And pay attention to eliminate expired object references, otherwise it may cause oom.

Manual cleaning usually uses try finally... Such a code structure.

Examples are as follows:

For the reference of expired objects, there are usually three common cases of oom. These three cases are usually difficult to find, and there will be no problem running in a short time. However, after a long time, the increase of leaked objects will eventually cause the program to crash.

When the class manages its own memory, it should be vigilant against memory leakage. Examples are as follows:

The reason for memory leakage is that even if other objects in the program are no longer referenced by those objects out of the stack, the elements [] array in the stack class still stores the references of these objects, so these objects will not be recycled by garbage collection. Therefore, when the class needs to manage its own memory, it is necessary to be vigilant about whether these expired references maintained internally are dereferenced in time, In this example, you only need to display the

elements[size] = null; Just.

Cache is to guard against memory leakage. In this case, once the objects are put into the cache, they are likely not to be used for a long time and are easy to forget. Usually, wakehashmap can be used to represent the cache. After the items in the cache expire, they can be deleted automatically. Alternatively, it can be periodically executed by a background thread to clear the expired items in the buffer.

For the registration of listeners or callbacks, it is best to display the deregistration. 2. Do not manually call finalize (), which is called for the garbage collector

3. Avoid using the finalize () method, unless it is used as a termination condition to find the part of the object that has not been properly cleaned up; It is used as a safety net to clean up system resources in the case of manual cleaning and forgetting to call. It is better to never clean up in case of delayed cleaning. If the information of forgetting to clean up resources is recorded at the same time, it is also convenient to find errors later and modify the code of forgetting to clean up in time; Release the non critical system resources obtained by the local methods in the object.

Because its execution time and whether it is determined to be executed cannot be accurately guaranteed, the finalize () method is best not to release key resources, but it can be used in the above three cases. The first case is as follows:

Execution result:

Error: the book object in the check out example must be in the checkin state before release, otherwise it cannot be released. The implementation in finalize can help find illegal objects in time, or more directly, use a reference variable reference directly in finalize to re-enter the reachable state, and then process it again.

Another thing to note is that if the subclass overrides the finalize method of the parent class, but forgets to call super. Manually An exception occurred in the finalize or subclass finalize process, resulting in no execution to super When finalize, the finalization method of the parent class will never be called to.

As follows:

Operation results:

Son finalize start or

Execution result:

Son finalize start for the second case, you can use try finally... Structure solution, but for the first case, it is best to use a method called the end method guardian. Examples are as follows

Execution result:

Execute the logic son2 finalize start in the parent class termination method here, so as to ensure that the operations required in the parent class termination method are executed to.

The above is the whole content of this article. I hope it will be helpful to your study.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>