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.