List of related Reading Notes
No. 4 avoid duplicate objects
If an object is non-mutable, it can be reused, for example:
- // Not recommended. "test" is a string instance. If this method is in a loop or frequently called, it will seriously affect the performance.
- String S = new string ("test ");
- // Recommended method
- String S = "test ";
For non-variable classes that provide static methods and constructors, we recommend using static methods to avoid repeated object creation. For example, the Boolean. vauleof (string) method is better than the constructor Boolean (string) method)
The following person class needs to create a new object each time the isbabyboomer () method is called, which greatly affects the performance.
- Import java. util .*;
- Public class person {
- Private final date birthdate;
- Public Person (date birthdate ){
- // Defensive copy-see item 39
- This. Birthdate = new date (birthdate. gettime ());
- }
- // Other fields, Methods omitted
- // Don't do this!
- Public Boolean isbabyboomer (){
- // Unnecessary allocation of expensive object
- Calendar gmtcal =
- Calendar. getinstance (timezone. gettimezone ("GMT "));
- Gmtcal. Set (1946, calendar. January, 1, 0, 0, 0 );
- Date boomstart = gmtcal. gettime ();
- Gmtcal. Set (1965, calendar. January, 1, 0, 0, 0 );
- Date boomend = gmtcal. gettime ();
- Return birthdate. compareto (boomstart)> = 0 &&
- Birthdate. compareto (boomend) <0;
- }
- }
The improvement plan is as follows:
- Import java. util .*;
- Class person {
- Private final date birthdate;
- Public Person (date birthdate ){
- // Defensive copy-see item 39
- This. Birthdate = new date (birthdate. gettime ());
- }
- // Other fields, Methods
- /**
- * The starting and ending dates of the baby boom.
- */
- Private Static final date boom_start;
- Private Static final date boom_end;
- Static {
- Calendar gmtcal =
- Calendar. getinstance (timezone. gettimezone ("GMT "));
- Gmtcal. Set (1946, calendar. January, 1, 0, 0, 0 );
- Boom_start = gmtcal. gettime ();
- Gmtcal. Set (1965, calendar. January, 1, 0, 0, 0 );
- Boom_end = gmtcal. gettime ();
- }
- Public Boolean isbabyboomer (){
- Return birthdate. compareto (boom_start)> = 0 &&
- Birthdate. compareto (boom_end) <0;
- }
- }
In this way, the class variables only need to be created once when the class is initialized to create calendar, date, and timezone. Initialization is no longer performed, and the performance is one hundred times better than that of the previous method. [Static variables are not initialized (delayed initialization) until the isbabyboomer () method is called )]
Class initialization sequence: initialize the static code of the parent class ---> initialize the static code of the subclass --> initialize the non-static code of the parent class ---> initialize the parent class constructor ---> initialize the non-static code of the subclass ---> initialize subclass constructor.
No. 5 Eliminate expired object references
The garbage collector does not recycle objects with "expired references" (which are never released. For example, if the element in the array is increased first and then reduced, the part whose subscript is greater than size () is the object that has expired the reference.
Solution:
- Public object POP (){
- If (size = 0 ){
- Throw new emptystackexception ();
- }
- Object result = elements [-- size];
- // Set the original reference to null after auto-Subtraction
- Elements [size] = NULL;
- Return result;
- }
Advantages: 1. Avoid system crash caused by memory leakage (Memory leakage is also common in cache, because the cache does not promptly clear useless entries, you can use weakhashmap to avoid this situation, refer: use weakhashmap to avoid Memory leakage caused by cache entry expiration );
2. The program can throw a null pointer exception immediately;
No. 6 avoid using the finalizer function to reclaim inaccessible objects. That is to say, after the end of the object's lifecycle, you can use the finalizer function to reclaim resources allocated to the object. However, the priority of the termination function execution thread is so low that we do not dare to recycle objects with high time requirements for the termination function. The JVM always delays the execution of the final function. For objects that urgently need to be recycled, you can use tyr
Finally: write the code to recycle objects in finally, so that objects can be recycled in time. Terminator functions are also useful. The first case is as a security net. When you forget to recycle the object, use the final function as the final security barrier. The second case is that a common object uses a local method.
The delegate to a local object is called a local peer. The local peer object is not a common object, so the local peer object will not be recycled when the object entrusted to it is recycled, so the final function will come in handy. At last, it should be noted that when the subclass rewrite overwrites the termination function of the superclass, if the call of the termination function of the superclass is not displayed, the termination function of the superclass will not be executed. Summary: try not to use the final function unless it is used as a security net or used to reclaim unimportant local resources.