My Thinking In Java Learning Notes (7) (ZT)

xiaoxiao2021-03-06  87

Cleaning: Termination and garbage recycling here to understand this time, the garbage collector can only recover the object generated by new, if you use or generate non-New generated objects, the garbage collector does not know how to clean him . This time you want to use Finalize (). 2, the transportation method of the garbage collector is this. When the garbage collector intends to start the release of the resources occupied by your object, first call finalize (), and reclaim the object when the next garbage recycling movement is released. The resource occupied, if you use Finalize (), he will make you perform your own cleanup action when garbage is recycled 3, Finalize () does not automatically evoke when the object is destroyed. Suppose there is an object to draw yourself on the screen during the generated process. If you don't have a manual to clean him, then he will never be there, and if you put the function of the cleanup screen in Finalize (), then when this object is recycled by the garbage collection, he is on the screen. The image will be cleared first. Your object will never be recycled because your program does not take system resources to take the status of the rubbish collection, and the system resources you take place will run when the program ends. All released, then you don't have to pay additional system expenses running in the garbage collector. What is the finalize () existent? To know, Finalize () is not used for general cleaning, the garbage collector returns to the objects generated by using New, and those unconventional methods (C / C native functions) will need Finalize () to clean up work . In this case, we should not use Finalize () because he is not a place where the regular cleaning action is placed. What do you have to perform a cleanup action is the execution site of normal cleaning action? In C , when the object is created in the STACK, then a destructor is automatically called before the end of the brace range where the object is located, if this object is in the New HEAP If you created, then you must call the Delete operator to clean up the object. Java does not allow objects to be created from the Stack, you must use New from the HEAP to create an object, but Java does not have a delete operator, but the garbage collector to clean the object, so we can say, since There is a garbage collector in Java, which does not require a destructive function. However, the garbage collector is not a destructive function after all, he can't replace the role of the arctive function. If you have other cleanup actions in addition to the release space, then you have to call a function similar to the destructor.

Death conditions As long as the program does not rely on call finalize (), then this function has one purpose, it is a check for the death of objects. So what is death? When the object is cleaned up, the object must be in some state to be cleaned up, that is, if there is a data stream that is no longer used, you need to clean up, then you have to be cleaned up. Close it, this is a kind of death condition

Class myclass {boolean b = false; myclass (boolean b) {this.b = b;} void death () {b = false;} public void finalize () {if (b) // Death condition is B Must be False System.out.println ("Error: b is true, class can't be clean"); Else System.Out.println ("OK! Cleanup");}}}} Class test {public static void main (String args " ) {MyClass MC = New MyClass (TRUE); mc.death (); new myclass (true); system.gc (); //system.gc () is used to force the end of the action, but even if he uses him , As long as you can occupy system resources to take a rubbish, finalize () will still be executed.

The transport of garbage collectors has learned that people who have learned procedures, from the practice of creating objects (except basic data types), which will greatly affect the speed of the system, but the garbage collector can be greatly improved. From the efficiency of the object from HEAP, you may be very surprised, the release of the garbage collector's storage space can affect the allocation of the storage space, and it is more surprised, this speed of the way to create an object from HEAP is approached. The speed of other languages ​​is created from the Stack! Why do this? Imagine an unlucky guy (object) standing on a bus (C HEAP), and other people have seats, he needs to keep there in the car, I hope to have no one to do the seat (high price ), A single person (object) leaves the seat (released), he will rush to sit down. In Java's JVM, the bus's seat turned into a belt. When there is an empty seat, the latter people make up the front seat, and the unfortunate person will take the last flight. The seat will be on, but don't need him to stay around. The garbage collector rearranges all objects in the Heap, allowing them to be more closely arranged, so that the Heap pointer can be moved closer to the front section of the transfer belt, avoiding the internal deposit page replacement action, and the efficiency is greatly improved. GC In order to achieve a faster garbage collection speed, he will find whether the handle of each object is pointed to a Heap to determine whether the object is referenced, whether it is cleaned, and the change will be re-mapped. . Because of this particularity of GC, when the GC is started, the execution program will temporarily stop. Members Initialization In Java, the data member variable of the Class's basic data type will be initialized automatically, and when the variable is defined in the function, he will not be initialized by the system.

Class test {BYTE B; Char C; Short S; INT i; long L; float f; double d; boolean bl; void Go () {INT II; //system.out.println (II); as variable is defined In the function, he will not be initialized by the system II = 10; System.out.Println (II); system.out.print ("Byte:" B "/ N" "char:" C "/ N" "" S "/ N" "INT:" i "/ n" "long:" "/ n" "float:" f "/ n" "Double:" D "/ N" "Boolean:" BL "/ n");} public static void main (string args []) {test t = new test (); t.go (); }}

10 byte: 0 char: CHAR value is 0, showing blank short: 0 int: 0 long: 0 float: 0.0 Double: 0.0 Boolean: false

Specify how the initial value specifies the beginning, this I think I don't have to write it ~ This one must know that the constructor can be used to initialize the action constructor to perform the initialization action, so you have a greater flexibility, but constructor The initialization action is after the automatic initialization

Class test {INT i; test () {i = 10;}} then, I will first initialize 0, and then the constructor is initialized to 10, and when the variable is defined, the initial value is given. Also, this requires that the initialization order of the initialization variables depends on the order of the defined variables in the Class, and the variable may be scattered through the functions, but all the variables must be in any function, even what is constructor Finished before being called

Initialization of static data, the data initial data of Static's basic data type and Non-Static's basic data type are not different, but if he is a handle of an object, then the initial value is NULL. The initialization of Static will only happen when necessary. If you do not produce a Class object, there is no static data, but Static's data will never be initialized. STATIC's initialization only occurs when the first Static's access action occurs, STATIC objects will not be initialized if Static does not initialize when the object is generated, the initialization order of the variables will become static Non-Static, MethodStatic Explicitly Initialization Java allows you to organize multiple static variables, put them in the Static block

Static {Int i = 10; byte b = 20;} When you first generate Class objects, or call the Static member of the class for the first time, Static block is initialized.

Non-Static Entity Initialization Action Java also provides a method of static variable initialization for non-static variables.

{INT i = 10; byte b = 10;} If we want to have an unknown internal hidden, this method is necessary (Chapter 8 will say)

转载请注明原文地址:https://www.9cbs.com/read-122590.html

New Post(0)