Android about destroying what you should know

Source: Internet
Author: User

What is the purpose of finalize ()

Five steps to read:

We all know the importance of initialization, and often forget the same important cleanup work. There is a garbage collector in Java that is responsible for (GC) reclaiming memory resources occupied by useless objects.

However, there are special cases: assume that your object (not using new) obtains a "special" area of memory. To handle this scenario, Java allows you to define a method named in the class finalize() .

It works "hypothetically": once the garbage collection period is ready to release the storage space occupied by the object, the method is called first finalize() , and the memory occupied by the object is actually reclaimed when the next garbage collection action occurs. So, if you are going to use a finalize() method, you can do some important cleanup at the time of garbage collection.

Note, however, that as long as the program is not on the verge of storage space, the space occupied by the object is not always released. If the program execution finishes and the garbage collector has not freed up any objects you create, the resources will all be exchanged to the operating system as the program exits. The garbage collector is only likely to be called when the program memory is tight, which is appropriate because garbage collection itself has a cost, and if you don't use it, you don't have to pay for it.

Keep in mind that neither "garbage collection" nor "finalization ()" is guaranteed to occur. If the JVM is not running out of memory, it will not waste time performing garbage collection to recover memory (in addition to manually calling System.GC () to enforce Finalize ().

The uncertainty of the execution of the garbage collector leads to the timing of the execution of the finalize() method is also uncertain, so, the method can not be finazlie() used as a general cleaning method.

So, finalize() what is the real purpose?

    • Garbage collection is about memory.

That is, the only reason to use the garbage collector is to reclaim memory that is no longer in use. So for any behavior related to garbage collection (especially finalize() ), it must also be related to memory and its recovery.

But does this mean that if there are other ordinary objects in the object (created by new), finalize() then the objects should be released?

The garbage collector is responsible for releasing the memory that the object occupies by creating the normal object (the new way). This limits the finalize() requirement to a special case, that is, the storage space allocated to an object in a way other than the way it was created.

But everything in Java is an object, so what is this special case?

The reason for finalize() this is that it is possible to allocate memory in a way similar to the C language, rather than the Java Common practice (new). This happens mainly when using the "native method", where the local approach (native) is a way to call non-Java code in Java, and currently only supports C or C + + (that is, the JNI or NDK programming that we often call).

In non-Java code, the function of C may be called malloc() to allocate storage space, and unless a free() function is called, the storage space will not be freed, resulting in a memory leak.

How the GC Works
// TODO
Destroy operations in inheritance

The order of destruction should be reversed in order to prevent a child object from being dependent on other objects.

For fields, it means that the order of the declarations is reversed, because the fields are initialized in the order in which they are declared.

For a parent class, the child class should be destroyed first, and then the parent class. This is because the destruction of the parent class might call some of the methods of the parent class, so the artifacts in the parent class need to still work, and they should not be destroyed prematurely.

Although usually we do not need to perform the destruction operation, but once the choice to execute, you must be careful and careful.

Android about destroying what you should know

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.