When I recently read the book "Java and patterns", I found that the section on Singleton pattern contains the following:
What the author wants to express is: to realize that an object can be permanently stored in the memoryProgramIt can be accessed throughout the running cycle, so that a member variable of the object can hold a reference pointing to itself to avoid being recycled.
To clear a member variable, you need to wait for the object to be released, and the object to be released does not need to be referenced. In this case, the member variable points to the object itself, which looks good, A loop is formed.
However, in fact, this statement is inaccurate and may cause readers to misunderstand it.
Please refer to the following sectionCode:
1 Package Com. cnblogs. test; 2 3 Public Class Singletontest { 4 5 Public Static Void Main (string [] ARGs ){ 6 7 // Call the test method 8 Test (); 9 // Notify JVM to recycle useless Resources 10 System. GC (); 11 12 System. Out. println ("Main finalize ..." ); 13 } 14 15 // Test Method 16 Public Static Void Test (){ 17 // Create a Class A Object 18 A A = New A (); 19 // Let the object member variables point to itself 20 A. _ A = A; 21 } 22 23 } 24 25 Class A { 26 27 // Defines a member variable to save the object itself. 28 Public A _ A = Null ; 29 30 // Method executed when the object is destroyed 31 Protected Void Finalize ()Throws Throwable { 32 System. Out. println ("A finalize ..." ); 33 } 34 }
Brief description:
When the test method is called in the main method, the test method creates an instance a of Class A and places the heap address of instance A in the member Variable _ A of instance, that is, when the simulated member variable holds a reference pointing to its own object.
After the preceding steps are completed, the test method ends. Because a is a local variable and is saved in the method stack, it is immediately released and no longer points to an instance of Class, however, we have just completed the "self-reference". According to the above theory, an instance with reference to Class A will not be released, therefore, the output result of the above program is "the main method has been executed... ".
Unfortunately, this is not the case. The actual output result is: "The main method has been executed... A is recycled ...".
Why? Listen to the explanation of the dish.
Objects Created in singleton mode can always exist in the memory and are not released, not just because they hold a reference, but because they are essentiallyThe reference is static.! That is to say, if the member variable is non-static and holds a reference, the object will still be recycled.
"At least one reference to an object is kept in the system". This reference refers to a reference issued from the stack or method area, that is, a secure reference.
After the class is instantiated, it is placed in the heap zone, and we cannot directly operate on the heap memory! Therefore, we need a reference pointing to a region in the heap zone, which must be issued from the stack (or method zone) because we can directly access the stack memory, if it is a reference from the heap, it is meaningless reference, and we cannot access it at all, so it will be recycled.
The class member variables are stored in the heap memory. Therefore, the class member variables hold an object reference. This reference is insecure (Insecure? invalid )!!
For another example, if the local variable A in the stack area points to the B object in the heap, a member variable of the B object points to the C object in the heap. We can access B through a and then access C by B. If a no longer points to B, then B will no longer be accessed, and C will not be accessed. Even if B still points to C at this time, it will become an inaccessible object, B and C will be automatically recycled by JVM.
In Singleton mode, the member variable is static. It is not stored in heap memory, but in the method area. It is a persistent memory space and will not be automatically recycled, therefore, it is safe to direct the reference to itself and it will not be recycled.
At this point, if we change the member variables in the first example to static ones, the objects of Class A will not be recycled after the test method is executed, and the output result of the program is: "The main method has been executed... ".
Finally, the heap area is shared by the entire program, so thread security issues may occur.