On the memory structure JVM Java basic knowledge and common interview questions

(1) .Sun HotSpot VM, and Open JDK is JDK comes with a virtual machine, is currently the most widely used Java Virtual Machine.
(2) .JVM Memory Distribution
Here Insert Picture Description
Program Counter : is a small memory space, the line number can be seen as an indicator of the current thread bytecode executed. The program branches, loops, jumps, exception handling, thread resume and other basic functions need to rely on this counter is complete. Since the multi-thread mode are alternately switched and allocated processor execution time is achieved by a thread, so that the thread private memory region.
VM stack : Java memory model is described a method of execution, used to store the local variable table, the operand stack, dynamic linking method exports
** heap: ** is the largest piece of memory in the Java virtual machine management, Java heap is shared by all threads in a memory area created when the virtual machine starts, the storage instance, the garbage collector is the main management
method area : used to store class information has been loaded in the virtual machine, constants, static variables, code data such as real-time compilation. GC HotSVM also for the region, and based mainly constant recycling
** (3) common interview questions

1. Q: jvm memory area which is private to the thread of which is shared by the threads **??
Thread private :
the program counter (Program Counter Register)
virtual stack machine (VM Stack)
native method stacks (Native Method Stack)

each while the thread is created JVM stack is created, stored in the JVM stack for the current thread in the local basic types of variables
shared by the threads :
the heap (heap)
which is used to store the JVM object instance and an array of values in the area, all you can think Java in this memory object allocated by the new created objects in the heap memory to wait for recovery GC
method area (method area)
the method of storage area for the loaded class information (name, modifiers, etc.), class static variable, a class is defined as the final type of constants, process the information in the Field class information class
direct memory (a portion of the non-runtime data area)

2. Q: JVM memory allocation strategy?
A: memory allocation object, in the general direction, is allocated in the Java heap.
In most cases, the new generation of object allocation in Eden area, Eden District when there is not enough space allocated, the virtual machine will launch a Minor GC.
In most cases, large objects directly into the old era, virtual machines provide parameters to define the threshold of the large object, the object will be directly above the threshold of entering the old era.
After objects (long-term survival of the object) after multiple Minor GC still alive, we will enter the old era. VM provides the parameters, you can set thresholds.

3. Q: how jvm tuning?
A: 1 to provide immediate garbage collection.
With Heap View function, we generally can successfully address the following issues:
- the size of the old generation of the young generation divide is reasonable
- memory leaks
- garbage collection algorithm set is reasonable
to view the heap space allocation (the young generation, old generation, permanent generation distribution)
garbage collection monitoring (monitoring recovery of long)
thread information monitoring: system thread number
thread status monitoring: each thread in what under the kind of state
thread Details: check the operation of the internal thread, deadlock checking
CPU hotspot: check the system which means taking up a lot of CPU time
memory hot spots: check what the maximum number of objects in the system

? 4. Examples of memory leak
A: Suppose you create an object, but the object has not been used, but this target has been applied, it will lead to useless but it will not be recovered

5.jvm heap and stack What is the difference? Please describe the purpose of each?
Stack memory means the program enters a method, assigns the local variable belongs to a private memory space for a separate internal storage for the method of this method,
when the end of this method, the stack allocated to this method will release the stack the variable will also release.
Heap and stack effect is different memory, generally used for storing data that is not on the current method stack,
for example, the use of new objects are created on the heap, so it will not disappear with the end of the process. After the local variables used in the method of final modification, on the heap, instead of the stack.

? Specific analysis 6.jvm heap
A: the young generation: All newly created objects are placed first the young generation. The young generation's goal is to collect as quick off the short life cycle of those objects
old year: experienced objects N times (configurable) garbage collection is still alive in the young generation, it will be copied to the older generation. Therefore, it is considered the old generation are stored in some of the longer life cycle of the object.
Garbage collection for old generation that is Full GC.
Permanent generation: the type used to store static data, such as Java Class, Method, etc.

? 7JAVA class life cycle of
Java classes begin to be loaded into the virtual machine memory, up to unload a memory, its entire life cycle, including: loading, validation, preparation, parsing, initialization, use, unloading seven stages.
A: Loading: Information obtained from the class file and loaded into JVM memory (method area, constant area) inside.
Verifying: checking whether the read structure description language specification and Java JVM specification. This is the most complicated class loading process, and the time it takes longest. Most use cases and scenarios JVM TCK tool is also used to test whether an error occurs in loading the wrong class time.
Preparing: a distribution information storage class structure is used, this structure contains a member variable defined in the class, method and interface information.
Resolving: all the symbols of the class constant pool reference changed to a direct reference.
Initializing: the class variable is initialized to an appropriate value. Perform static initialization procedure, the static variable is initialized to the specified value.

8. What is GC?
GC is a garbage collection means (Gabage Collection), memory handling is where programmers prone to problems,
forget or memory error recovery procedures can result in instability or even collapse of the system, provided Java GC function can automatically monitor whether the object is more than scope so as to achieve the purpose of automatically reclaims the memory,
the Java language does not provide a display method operation of the release of allocated memory.
Serial collector : the collector is a single-threaded, it will only use one CPU or a collection thread to complete garbage collection, garbage collection is performed, must suspend all other work thread until it collects end.
ParNew collector : a multithreaded version Serial collectors, in addition to the use of multiple threads for garbage collection, the other collector behavior and Serial exactly the same.
CMS collector : a pause for the shortest recovery time objectives collector. Process is divided into the following four steps:
initial marking
concurrent mark
relabeled
concurrent Clear

9.GC will first clean up the data heap of data or stack?
A: The general store heap objects you create, such as calling new XXX
and stored in the stack is generally a local variable
stack is a thread private, and heap data It is shared.
GC only interested in the data heap, it will be real-time monitoring object in the heap

? 10.JVM garbage collection algorithm
mark - sweep algorithm : First all need to be recovered objects, unifying all objects are marked recovery after labeling is complete.
Replication algorithm : by available memory capacity is divided into two equal size, uses only one of them. When one runs out of memory, will also keep on top of another piece, then clean out once in the memory space has been used.
Mark - sorting algorithm : the labeling process and "mark - sweep" algorithm is the same, but the subsequent steps are not directly recycled objects to clean up, but to the end of the move, and then clean out the memory directly outside the terminal boundary.
Generational collection algorithm : the Java heap is generally divided into the old and the new generation's, using the most appropriate collection method based on the characteristics of each era. The new generation of large numbers of objects are found to have died, the choice of replication algorithm. Old era because of the high survival rate of the object, you must use the "mark - clean-up" or "mark - finishing" algorithm to recover.

? 11.JVM common startup parameters
-Xms / -Xmx - initial heap size / maximum heap size
-Xmn - the young generation heap size
-XX: -DisableExplicitGC - let System.gc () does not have any effect
-XX: + PrintGCDetails - GC print details of
-XX: + PrintGCDateStamps - stamp printing operation GC
-XX: NewSize / XX: MaxNewSize - set new generation size / maximum size CENOZOIC
-XX: NewRatio - may be provided and the new generation Older Generation the proportion of
-XX: PrintTenuringDistribution - set each output target age of the survivors in the park after the new generation GC distribution
-XX: InitialTenuringThreshold / -XX: MaxTenuringThreshold: set the initial value of the old year and the maximum threshold
-XX: TargetSurvivorRatio: set survived target utilization zone

Creation 12.Java object?
Here Insert Picture Description
Step1: Check the class loading
when a new virtual opportunity to instruction, first to check whether the parameters of this command can locate the symbol of this class in the constant pool references, and check this symbolic references on behalf of whether through class it has been loaded, parsed and initialized. If not, then you must perform the appropriate class loading process.

Step2: allocate memory
after examination by the class loader, then the virtual machine will newborn object allocation memory. Object required memory size can be determined after the class is loaded, the space allocated for the task object is equivalent to determine the size of a piece of memory from the Java heap carved out. There are distribution "pointer collision" and two kinds of "free list", choose the kind of distribution of the Java heap decide whether structured, and whether regular Java heap and used by the garbage collector with or without compression decided to organize functions.

Memory allocation in two ways:
which one to select more than two ways, depending on whether the regular Java heap memory. And if regular Java heap memory, depending on GC collector algorithm is "mark - sweep" or "mark - finishing" (also called "mark - Compression"), it is worth noting that the memory is structured replication algorithm

Memory allocation concurrency issues
when creating the object there is a very important issue, that is thread-safe, because in the actual development process, creating objects is very frequent thing, as a virtual machine, it is necessary to ensure the safety of the thread is usually in terms of virtual machine used in two ways to ensure thread safety:

Step3: initialization value of zero
after the memory allocation is completed, the virtual machine needs to allocate memory space is initialized to a value of zero (not including the object header), this step ensures that the object instance field may not be assigned an initial value to the Java code directly, the program can access the data type to the zero value corresponding to these fields.

Step4: Set the object head
after zero value initialization is complete, the virtual machine is subject to the necessary settings, for example, this object is an instance of that class, how to find information such as metadata, hash code of the object, GC generational object age and other information. This information is stored in the object header. In addition, depending on the current operating status of the virtual machine, such as whether to enable biased lock, head objects have different set up.

Step5: the init method
after the completion of the above work, from the perspective of the virtual machine's point of view, a new object has been created, but from the perspective of the Java programming point of view, object creation has just begun, the method has not been performed, all the fields still zero. So, in general, new instruction after execution of the method is then performed, to initialize the object in accordance with the wishes of the programmer, such an object is considered completely available truly emerge.

Published 99 original articles · won praise 2 · Views 2632

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/104885340