"Java Virtual machine" must be known--14 issues Summary (memory model +GC)

Source: Internet
Author: User
Tags exception handling garbage collection object object stringbuffer
First, Java overview
1, Java compared to PHP, C #, Ruby and so on as a very good programming language advantages. (1) The system structure is neutral, the cross-platform performance is superior. The Java program relies on the JVM to run, the Javac compiler compiles the Java program as a platform-specific bytecode file (. Class), and the JVM matches the different operating systems, loading the bytecode and interpreting (and possibly compiling, in the third question) the execution of the machine instruction. (2) superior safety. The JVM is isolated from the host environment, and the Java syntax guarantees security to some extent, such as discarded pointer operations, automatic memory management, exception handling mechanisms, and so on. (3) multithreading. Prevents single thread blocking from causing the program to crash, distributing tasks, and improving execution efficiency. (4) distributed. Support distributed, improve application system performance. (5) Rich third-party open source components. Spring, Struts, Hibernate, MyBatis, quartz, and so on.
2, what is the byte code?. class bytecode file is what. (1) bytecode is a sequence of symbols that contains Java internal instruction sets, symbolic sets, and some ancillary information that can be identified and interpreted by the JVM. The bytecode does not contain any delimiters that distinguish between paragraphs, and data of different lengths are constructed into N 8-byte units. (2). Class is stored in the Java program is compiled bytecode, contains the class version information, fields, methods, interfaces and other descriptive information and a constant pool table, a group of 8-byte units of bytes composed of a byte code file.
3, what is the JVM. What are the characteristics of the hotspot virtual machine? JVM Full name Java Visual Machine,java virtual machine. Java program is the operating environment, mainly responsible for loading bytecode files, and interpreted or compiled into the corresponding platform of machine instruction execution. The most we use is the JDK default hotspot virtual machine, which uses an interpreter plus a compile-time coexistence architecture scenario. The interpreter is used at the outset, so that the bytecode can be interpreted as a local machine instruction execution at the end of the compilation to improve efficiency. Compilers are the hot research of hotspotfunction, in the presence of frequently invoked methods or more cycles of code, this block of code will be labeled as "Hotspot Code", through the embedded double JIT (Just in time compiler) to compile the bytecode directly into the corresponding machine instructions to improve efficiency.
Second, Java memory model
1. PC CounterThe thread is private, used to record the address of the current thread executing the bytecode, and if the native local method is executing, the PC counter is empty.
2, Java stackThread-Private, also known as Java Virtual Machine stack, used to store stack frames, stack frame into the stack process is the process of the method call to the execution of the end. The main storage method in the stack frame is the local variable table (including the declaring data type of the local variable, the object reference, etc.), the operand stack, the method export and so on.
3. Local method StackSimilar to Java stack functionality, it is used to store information about native local methods.
4, Java heapThreads are common and are used to hold object instances, including arrays, also called GC areas, which are the main areas of GC work. Also so, because the GC frequency is too fast and inefficient, the heap area may become the JVM performance bottleneck, so considering the performance, the heap area is no longer the only choice of object memory allocation. Here it involves the object's escape analysis and stack allocation. Escape analysis is used to analyze whether the scope of an object is within a method, escaping when the method returns the current class instance object, assigning values in the method to the current class member variable, and referencing the current class member variable in the method, and still allocating memory on the heap. However, when the scope of the object is within the method, such as creating an instance of the class within the method, no return, no reference, this situation directly allocates memory on the Java stack, releasing the space with stack frame and relieving the pressure of GC in the heap area.
5. Method AreaThreads are common, storing the structural information for each Java class, such as fields, bytecode content data for various methods, and running constant pools. The method area is also known as a permanent band. Generally, there is no requirement to display, and GC is only for constant pool reclamation and type unloading in the method area.
6, the operation of the constant amount of poolPart of the method area, when the class loader loads a class's bytecode file into the JVM, converts the constant pool table in the bytecode file into a run-time pool.
Third, Java garbage collection mechanism
1. What are the common algorithms for labeling available objects? (1) Reference counting method:Each object creates a private reference counter that is referenced by another object (appearing on the right side of the equal sign), a reference counter plus 1, and a reference counter minus one when it is no longer referenced, and the object can be reclaimed when the reference counter is 0 o'clock. In this way, when two objects are referenced to each other, the two reference counter values are not 0 can not be recycled; (2) Root search algorithm:The notation algorithm commonly used by the JVM to treat the reference of an object as a graph structure, starting from the root node set, the unreachable node can be recycled, where the root node collection contains the following 5 elements: 1, object references in the Java stack, 2, object references in the local method stack, 3, object references to run a regular pool, 4, The object reference of the static property in the method area; 5, all Class objects;
2. What are the common garbage collection algorithms? Which type of JVM to use. (1) Mark-Purge algorithm:Executed in two phases, the first phase marks the available objects, the second phase clears the garbage object; This method is simple but inefficient, and produces memory fragmentation (discontinuous memory space) that cannot be allocated to larger objects again. (2) Replication algorithm:is widely used in the recovery of new generation objects. The memory is divided into two areas, the new objects are allocated in one area, the available objects are copied continuously to another area when the recycle is complete, the new objects are assigned to the area of the object, and the loop repeats. This algorithm does not produce memory fragmentation and is more efficient, but because only one area is valid at the same time, memory utilization is low. (3) mark-Finishing algorithm:is used in the collection of old age objects. This algorithm is similar to the tag cleanup algorithm, where the first stage marks the available objects, and the second phase moves the available objects to a contiguous memory, which solves the drawback that the tag-purge algorithm produces memory fragmentation. (4) Generational recovery algorithm:In the hotspot virtual machine, based on the characteristics of the generational (heap memory can be further divided into the younger generation, the old age, the old age to store long-lived objects), the JVM GC uses the generational recovery algorithm. Young Generation uses replication algorithms:is divided into a larger Eden area with two smaller, equal size survivor area (from the space and to spaces), the proportion is generally 8:1:1. New objects are assigned to the Eden area, and when GC occurs (the Cenozoic GC is generally called the minor GC), the Eden and the available objects in the from area are copied to the to area, and the from spaces and to spaces are interchanged with the name, looping the method. Until the following two situations occur, the object enters the old age: 1 ' objects in the from area have reached the survival algebraic threshold (the number of GC to achieve the set value), the GC will not enter into the to zone, directly moved to the old age; 2 ' after reclaiming the Eden area and the from area, it is beyond the reach area, Move the surviving object directly to the old age. old age using tag-collation algorithm:The full GC is triggered when the old age fills (the new generation is associated with the old age with GC).
3. What are the common garbage collector? What are the characteristics. Suitable for use with what scene. (1) Serial collectorThe younger generation uses replication algorithms, serial recycling, and the "Stop the World" mechanism (when GC stops everything else), applies to a single core CPU environment and is not recommended for server-side applications. Serial provides old-age recycler serial, using tag-sorting algorithms, and other features consistent with the new generation. Serial+serial old is suitable for client scenarios. (2) parnew collectorEquivalent to the serial version of multithreading, parallel recycling, the younger generation also uses the replication algorithm and "Stop the world" mechanism, suitable for multi-core CPUs, low latency environment, recommended for use in server scenarios. (3) Parallel collectorSimilar to Parnew, replication algorithms, parallel recycling, "Stop the world" mechanism, but unlike parnew, parallel can control program throughput size, also known as throughput priority garbage collector. Similar to serial, Parallel also has an older version, Parallel old, which also uses a tag-sorting algorithm. Parallel+parallel old is ideal for server scenarios. (4) CMS collectorWith the parallel of high throughput, the CMS is for high concurrency, low latency and health. Use tag-purge algorithm, parallel recycle, "Stop the World". Because the tag-purge algorithm is used, a large amount of memory fragmentation is generated and is used sparingly. (5) G1 collectorIt is a kind of regionalization garbage collector based on parallel, concurrent, low delay and controllable time. The revolutionary significance of the design, abandoned the heap area of the young generation, the old era of the division scheme, but the heap area or divided into about 2048 of the same size independent region block. Please refer to Importnew Bowen http://www.importnew.com/15311.html
4, the GC optimization scheme. The basic principle is to minimize waste and reduce the cost of the GC process as much as possible. It should be noted that the JVM has a high frequency of GC, but because the minor GC takes a very short time, it has little impact on the system. More noteworthy is the full GC trigger bar, which includes the following: (1) do not explicitly call System.GC ()Calling System.GC () is also just a request (recommended). When the JVM accepts this message, it does not do garbage collection immediately, but simply weights several garbage collection algorithms that make garbage collection operations easy to occur, occur earlier, or recycle more. But even then, in many cases it triggers the full GC, which increases the number of intermittent pauses. (2) minimizing the use of temporary objectsThe temporary object becomes garbage after it has been called out of the function call, and less temporary variables are equivalent to reducing the generation of garbage and reducing the probability of full GC. (3) It is best to explicitly null an object when it is not in useIn general, NULL objects are treated as garbage, so it is more efficient for GC collectors to determine garbage by explicitly setting unused objects to null. (4) try to use StringBuffer instead of string to accumulate stringsBecause string is a constant, when the string object is accumulated, it is not amplified in a string object, but rather a new string object, such as STR5=STR1+STR2+STR3+STR4, that produces multiple garbage objects during execution because of the second Action must create a new string object, but these transition objects are meaningless to the system and will only add more garbage. To avoid this situation, you can use StringBuffer to accumulate strings, because the StringBuffer is variable length, it expands on the original basis, does not produce intermediate objects. (5) can use basic type such as Int,long, do not use Integer,long objectThe base type variable consumes much less memory resources than the corresponding object, and if it is not necessary, it is best to use the base variable. (6) Use of static object variables as little as possibleStatic variables are global variables and are not reclaimed by GC, and they can occupy memory all the time. (7) time to create or delete dispersed objectsFocusing on a large number of new objects in a short time, especially large objects, can result in a sudden need for large amounts of memory, which, in the case of the JVM, can only be done in full GC to reclaim memory or consolidate memory fragmentation, thereby increasing the frequency of the primary GC. The same is true for deleting objects centrally. It causes a sudden emergence of a large number of garbage objects, the inevitable reduction of free space, thereby greatly increasing the next time to create a new object to force the main GC opportunity.
5, Java even if there is a GC will also occur memory leaks. An example is provided. 1. The use of static collection classes like HashMap, vectors, and so on is the most vulnerable to memory leaks, and the lifecycle of these static variables is consistent with the application, and all object objects cannot be released because they will also be applied by vectors.
Static vector v = new vector ();   for (int i = 1; i<100; i++) {Object o = new Object ();   V.add (o); o = null; }
In this example, there is a reference o to the vector object's Reference V and object in the code stack. In the For loop, we constantly generate new objects, add them to the vector object, and then place an O reference empty. The problem is that when the O reference is empty, if a GC occurs, the object we create can be recycled by GC. The answer is in the negative. Because the GC finds a V reference when it tracks a reference in the code stack, and continues to trace it, it finds a reference to the object in the memory space that the V reference points to. That is, although the O reference is already empty, the object object still has other references that can be accessed, so the GC cannot release it. If the object object has no effect on the program after this loop, then we think that the Java program has a memory leak.
2. A variety of connections, database connections, network connections, IO connections, etc. do not show the call close shutdown, the GC recovery is not caused by memory leaks.
3. The use of listeners can also cause memory leaks when the object is released without corresponding removal of the listener.





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.