Thinking in Java (III)-initialization and cleanup

Source: Internet
Author: User

Today I read Chapter 4

Initialization and clean up

Constructor is a natural and effective setting.

Before the constructor is called, the automatic initialization of the object is notProgramBut after the call, the object will be in the desired state.

About the argument list of Constructors

It can carry out wide transformation, but narrow transformation requires clear identification. For details, see the lengthy example in the book...

Memory clearing, which is different from C ++

The entire mechanism is described in three sentences.

1. Garbage collection is not equal to the analysis structure

2. Objects may not be recycled

3. Garbage collection only recycles memory

The garbage collection process is like this.

When heap memory is about to run out, the garbage collector is automatically started.

When it is determined that the object space can be released, that is, the object is not partially referenced, The finalize () object is called first, and then the memory of this segment is recycled.

For example, if an object draws a circle that meets the conditions for clearing the object space, the garbage collection process starts.

The object is released, but the circle still exists. This can be considered as a bug.

In fact, you need to write a method to erase the circle and complete all the aftermath except for releasing space.

Therefore, the finalize function is slightly embarrassing. It determines whether the object has reached the death condition and whether the space can be released.

If necessary, you can also add some output statements to observe the memory recovery status.

In this case, the garbage collector does the same thing and only recycles the garbage, rather than cleaning up the pollution caused by the garbage.

In addition, it is necessary to take the initiative to clean up when the memory is available,

Therefore, after running the program, it is likely that your object will still exist.

After all, the overhead of calling a garbage collection operation is still very large. Let it save a little garbage for the time being.

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.