Programmers are aware of the importance of initialization, but often forget the same important cleanup work, what is the purpose of this introduction to the garbage collection mechanism and the Finalize () method in Java?
Garbage collection
garbage collection isanother term for automatic memory management in J Ava. The purpose of garbage collection is to keep as many available heaps as possible for the program. The JVM deletes objects on the heap that are no longer required to be referenced from the heap.
Java garbage collection is done automatically by the JVM and is not controllable for programmers.
System.GC (); Sysytem.getruntime (). GC (); Calling both methods does not immediately garbage collection.
When do I run a garbage collection?
The possible scenarios for running garbage collection are:
1. Heap available memory is low
2.CPU Idle
Best Practices for garbage collection?
Programmatically, we can manually let the JVM run garbage collection by calling the System.GC () method.
The JVM may throw outofmemoryexception when the memory is full and no objects on the heap are available for garbage collection.
An object runs the Finalize () method before it is removed from the heap by garbage collection. We recommend that you do not write any code with the Finalize () method.
Why can't GC recycle stream objects?
The GC is the primary cleanup memory, but the Close method also frees up some system resources, such as the resources used by the hard disk to read and write.
In addition, not close, then the program is still running, no end, so the GC does not treat it as garbage, recycling.
If a stream does not shut down there will be some additional problems, such as locking a resource, if you open a file with a stream, not closed,
This file cannot be deleted, it will prompt a program to manipulate the file, and will also occupy the system handle.
What is the purpose of the Finalize () method?
The Java garbage collector knows only to release the memory that is allocated by new, so it does not know how to release this "special" Memory for the object, and in order to deal with this, Java allows a method named Finalize () to be defined in the class
, it works like this:
Once the garbage collector is ready to release the storage space occupied by the object, the Finalize () method is called first, and the memory occupied by the object is actually reclaimed when the next garbage collection action occurs. So if you're going to use Finalize (), you can
Do some important cleanup work at the time of garbage collection.
Summary:
1. objects may not be garbage collected.
2. garbage collection does not mean "destruction" (because of the existence of a garbage collection mechanism, so there is no destruction).
3. garbage collection is only related to memory.
That is, the only reason to use the garbage collector is to reclaim memory that the program is no longer using, so for any behavior related to garbage collection (especially the Finalize () method), they must also be related to memory and its recycling.
But does this mean that if the object contains other objects, finalize () should explicitly release those objects? No, regardless of how the object is created, the garbage collector is responsible for releasing all memory occupied by the object, which will require the Finalize ()
Restricts the allocation of storage space to an object in a way other than the way it was created.
4. you may find that as long as the program is not on the verge of storage space, the space occupied by the object will not be released, if the program execution ends, and the garbage collector has not released the storage space of any objects you create, the program's
Exit, those resources will be all handed over to the operating system, this strategy is appropriate, because the garbage collection itself has the overhead, if you do not use it, you do not have to pay for this part of the cost.
Java garbage collection mechanism