Object class Source Analysis

Source: Internet
Author: User
Object class

The object class is the root class of the class hierarchy. The object class is a superclass of each class. All objects, including arrays, implement the methods of this class.
The object class belongs to the Java.lang package, all classes inherit the object class directly or indirectly, and there are 11 methods in the Jdk1.6 version of the object class.
There are many native methods in the object class, also known as local methods, which are implemented in a dynamic library with C (c + +) and then invoked through JNI. Source Analysis

Package Java.lang;
    public class Object {private static native void Registernatives ();  static {registernatives (); Local method, which automatically calls this method}//1 when the object is initialized. Returns the Run-time class object for this object.  
    This object is also used for the monitor object of the static synchronized method of the class. 

    Public final native class<?> GetClass (); 2. Returns the hash value of this object, which returns the memory address of the object by default.
    Overriding this method can improve the performance of the collection of hashing structures, such as HashMap. 

    public native int hashcode (); 3.
    Object comparison method, which determines whether the current object is equal to other objects, and by default compares the addresses of two objects.
    The general overriding Equals method is overridden with the Hashcode method public boolean equals (Object obj) {return (this = = obj); 
    //4. Object Cloning methods are used for object replication, classes that support replication must implement the Cloneable interface, and executing the Clone method generates an exception if the Cloneable interface is not implemented. 

    Protected native Object clone () throws Clonenotsupportedexception; 5.
    Returns the string representation of the object by default, using the Hashcode value (memory address) of the class name +16, overriding this method to improve the readability of the class.  
    Public String toString () {return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ()); }//6. The thread wakes up (single) and wakes up a single thread waiting on this object monitor.
    Can be used to communicate with each other in collaboration between threads.   

    Public final native void Notify (); 7. Thread Wakeup(all), wakes all threads waiting on this object monitor. 

    Public final native void Notifyall (); 8. Thread wait, local method. 
    Timeout represents the maximum time, in milliseconds, to wait.

    Public final native void wait (long timeout) throws interruptedexception; 9. Thread wait, support nanosecond correction value, rounding public final void wait (long timeout, int nanos) throws Interruptedexception {//... Nanos 
    For extra time (in nanoseconds), the value range is 0~999999, and when nanos>=500000, the timeout value plus 1 wait (timeout); }//10.
    Thread-waiting, and the Notify method is a relative method, wait and nofity two methods to implement the thread communication, the two methods must be in the same monitor object's synchronized code block used.   
    Public final void Wait () throws interruptedexception {wait (0); }//11.
    Like a destructor, this method is called by the object's garbage collector to release resources when the garbage collector determines that there are no more references to the object.
    The finalized method is called by GC and also allows for a single regeneration (the object is again available in this thread after being marked finalized), which can slow down the GC speed and lead to frequent fullgc, or even direct oom.
    Finalize execution is indeterminate, not determining which thread is executing, or the order of execution.
    Because the Finalize method may affect the GC, phantomreference virtual references can be used instead of the Finalize functionality.
    Effective Java tells us that the best thing to do is to provide the close () method and to tell the top application to call such an interface when it is not needed. protected void Finalize () throws Throwable {}}
Finalize Method FlawOverriding the process of creating a Finalize Method object
Creates an instance of object a Java.lang.ref.Finalizer object instance f1,f1 points to a and a referencequeue, referencing the relationship: F1->a,f1->referencequeue ( Finalizer class static method creates a background thread to execute the Runfinalizer method from the queue The Java.lang.ref.Finalizer class object references F1 so that the F1 can never be recycled unless the finalizer class object's reference to F1 is lifted. After the above three steps, refer to the relationship: Java.lang.ref.finalizer–>f1–>a,f1–>referencequeue. To override the GC recycle process for a Finalize method object
When a minor GC occurs, even if an object A is not referenced by any other object, as long as it contains override Finalize (), it is eventually referenced by an object F1 the Java.lang.ref.Finalizer class If the new generation of objects all contain override Finalize (), then it is not possible to GC. Yes, that's the first risk for Finalize (); the minor GC copies all active objects and the (actual) garbage objects referenced by the Java.lang.ref.Finalizer class object to the next survivor area, if the copy overflows, The overflow of data is promoted to the old age, in extreme cases, the old age capacity will be quickly filled, so the headache of the frequent full GC is not far away from us. When an object is found in the first minor GC that is referenced only by a Java.lang.ref.Finalizer class object, the GC thread plugs the finalizer object that points to object a F1 into the F1 referenced by Referencequeue; The Java.lang.ref.Finalizer class object contains a Deamon thread finalizer that runs at a low level to invoke the Finalize () method of these objects asynchronously, and after the call is done, The Java.lang.ref.Finalizer class object clears its own reference to the F1. This allows the GC thread to reclaim object A at the next minor GC. It can be seen that the number of finalizer objects directly affects the speed of minor GC. The object recycling process for the finalizer method is summarized with the following three risks:
If any one of the Finalize () throws an exception, the finallize thread terminates, and soon the Oom finalizer thread runs at a low level due to referencequeue growth, and there is a possibility that the finalize speed is not up to date with the object creation speed , and may eventually be oom, in practice, there is usually rich CPU time, so this oom situation may not often occur with override Finalize () objects must undergo at least two GC to be recycled, seriously slow the GC speed, bad luck directly promoted to the old age, May cause frequent full GC, which in turn affects the performance and throughput of the system.

Reference
* Java object class source code read
* Java object class Source details
* Deep understanding of Java Finalize
* Finalize implementation principles and the resulting murders
* java Finalizer caused by the memory overflow

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.