A. Object creation process
1. virtual opportunities to new instructions, check the back of the new class notation, if you can find in the constant pool, and then check whether the class implementation of the class loading process, if not on the first execution class loader,
2. After the class loader to determine the memory to be allocated, and allocates memory based on the structure of the heap (collision pointer, free list), but also consider the issue of the allocation of concurrent, possibly two threads
Two objects assigned to a memory, the solution is applied using cas failure retry, or use TLAB (ThreadLocal), creating a thread to a thread when the heap a small space, like space
After the run out, then use the CAS plus retry failed to allocate memory.
3. Finally, the hash code, belonging to the class to which the object instance, the GC object generational age information is stored in the object header. At this point, the virtual machine to create the object is completed, the next step is to execute the constructor initialization process.
Composition of the object instance
Object header
1. Run the required data storage objects: object lock state, the hash code, the GC generational Age
2. Type pointer, this object is found through which instance belongs.
Object Content
Alignment padding
Some require the object size is an integer multiple of 8 bytes.
III. Use objects
The upper surface is a reference, but there are two types of references: a pointer handle.
1. Pointer
Is a direct reference to the object on the heap address, the advantage is access to fast, the downside is that objects are always changed in the heap address (because the garbage collection mechanism, replacement), it is necessary to modify the value of reference.
2. Handle
Handle table to point stack reference value, the deposit efficiency disadvantages handle table objects by address, and the address of the object's class (area method), without the benefits of variable reference pointer is not high.
IV. Methods of determining whether an object has survived
Reference counting
Object maintains an integer, there is a reference to a plus, minus a few citations, zero when he died. No, because there is a circular reference, the other two objects are only referenced.
Reachability analysis algorithm
V. reference type
There is such a class of objects, when memory is tight they can not survive when the memory is off, you can keep (such as caching feature), so there is a reference type, abandoned or have been cited references would have either not been recovered the two extremes.
Strong references: the normal use of a reference type.
Soft Quote: useful but not necessarily point to an object, when to take place before the memory overflow exception, these recovered.
Weak Reference: More weaker escape the garbage collection, the next gone.
Virtual reference: still recovered, when the sole function of issuing a notification recovery.
VI. Recycling process
Reachability algorithm found no one reference to an object, it will mark a bit, and then determine finalize () method if there is (an object can only be executed once), necessary to put these objects into f-queue queue , and then mark it, then
Virtual machines create a thread to perform them (does not guarantee a successful conclusion, because there are an infinite loop will affect other objects), this time if the reference chain finalize the energy and the establishment of the link (make a reference to the chain of object references it) it escaped, if not, it was recovered.
Seven .GC collection mechanism
Now most of the virtual machine is distinguished on behalf of the heap for recycling management.
New Generation: includes an eden region, two survivor zones. Using replication algorithm, minor GC.
Years old: old district. Mark - Collation Algorithm major GC.
fullGC full clean-up
Garbage collection process: When the deposit to eden zone object fails, it is at the discretion of reachability analysis algorithm, eden region and survivorfrom area live objects copied to survivorTo area, the process is called minorGC, and so it is not necessarily common eden Mancai use.
And so survive after minorGC execution object more than 15 times, the survivors still keep to the old years old years actually provide a mechanism to guarantee the normal 98% of the objects are dead Chaosheng evening, so the new generation region is set to 8: 1: 1, sufficient. Large Object (Array) or
Long survival time of the object's direct deposit to the old, the old trigger FullGC's full time to clean up the old era.
Will judge's ability to hold the new generation of old objects, can not be allowed if jiupanduanyigeflag not guarantee failure, if it is allowed to judge each of the previous years continued to check whether the maximum available contiguous space larger than the old before making minorGC average of the previous promotion to the subject of old age size, can be into, not on fullGC.
Reachability algorithm:
Six loading process
Steps: loading, validation, preparation, parsing, initialization, use and uninstall. Step 2.3.4 also known as connection
Perform the initialization time
When an object or by calling the static code 1.new
2. When creating objects through reflection
3. The implementation of the process of creating a subclass of the object, to perform initialization of the parent class
4. Start the virtual machine initialization initialization main belongs.
Loading: by converting the fully qualified class name for the class-based binary byte stream to generate class object.
Verification: byte stream file information to determine whether to meet the requirements, will endanger the java virtual machine itself.
Preparation: The class variable allocate memory (area method), and clears to zero.
Resolution: Set the constant pool of the class reference
Initialization: Initialization code executes the program.
Class loader: loading step execution class, find the class codes by using the fully qualified class name of the class, converting it into a binary byte stream.
Parent delegation model
The model refers to the inheritance system class loader (not really a combination of inheritance is implemented), then introduce the next class loader
Boot class loader: the virtual machine comes (C ++ written) can be loaded <java_HOME> / library is loaded into the virtual machine lib directory
Extension class loader: <java_HOME> / library under lib / ext directory
Application class loader: loading the library user class path, also called the system class loader.
Other user-written class loader: are the combination of inherited application class loader.
Class loading process: When a class loader to load receipt of the request, he commissioned the parent class loader process, the parent class men so, until the top level that I can not at this path load, the subclass will attempt to load.
The benefits of using parents delegated model: a class loader and by his own unique identification, if not this model, the two loader to load a class, there will be the same type of inconsistency problem, parents delegate mechanism to ensure multi when the loader loads a class, the final is loaded by a loader, ensure that the final load the same result.
Parents delegate damage model: Before jdk1.2 parent delegation model introduced before there will be a custom class loader, this time the user will override the loadClass () class were loaded, and after the introduction of the model, the method calls parent class loader can not be covered, so that it
He added a call for rewriting findclass method in this method.
2. In implementing jdbc: originally class.forName ()
Then manager management driver, but later,
// 1.加载数据访问驱动 Class.forName("com.mysql.jdbc.Driver"); //2.连接到数据"库"上去 Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK", "root", "");
public class Driver extends NonRegisteringDriver implements java.sql.Driver { public Driver() throws SQLException { } static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } } }
This is before, Class.forName () actually triggered the static code block, and then registered with the DriverManager in a mysql Driver's implementation.
But then
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK", "root", "");
Here you can see the direct access connection, eliminating the above Class.forName () registration process.
Now, we look at the analysis model using the services of this original spi What is the process:
- First, get a specific category name "com.mysql.jdbc.Driver" from the META-INF / services / java.sql.Driver file
- Second, load this class, there is certainly only with class.forName ( "com.mysql.jdbc.Driver") to load
Well, the question is, Class.forName () is Classloader loaded with the caller, the caller is in rt.jar the DriverManager, ClassLoader class loader is to start, but certainly not com.mysql.jdbc.Driver under <JAVA_HOME> / lib, so it must be unable to load mysql in this class. This is the parents delegate limitations of the model, the parent child class loader can not load paths in the class loader.
Thread context class loader () method provided by Thread.setContextClassLoaser, if special settings are not inherited from parent classes, typically used by default application class loader
Obviously, the thread context class loader allows the parent class loader can call the child class loader to load classes, which broke the principle of parental delegation model
Optimization locks: locks because java semaphore mechanism is used to implement an operating system, it is necessary to switch the kernel mode, and will cause a lock switching between threads, very time consuming, so the optimized lock.