First, the JVM startup process
1.java virtual machine-initiated commands are initiated by Java +xxx (the class name, the main method in this class), or JAVAW.
2. After executing the command, the first step of the system is to mount the configuration, which will look for the config configuration file of the JVM in the current path.
3. Locate the Jvm.dll file after locating the config configuration file for the JVM. This file is the main implementation of the Java Virtual machine.
4. When a jvm.dll file matching the current version is found, the DLL is used to initialize the JVM virtual machine. Get the relevant interface. Then find the main method to start running.
The above description of the process is relatively simple, but the JVM's start-up process is basically covered.
The basic structure of the JVM class loader subsystem is usually what we call the ClassLoader class loader, first we will be loaded into the JVM memory through ClassLoader, the local method area is mainly native method call, this we do not care about before, PC Register 1. Each thread has a PC register. The JVM assigns each thread a PC register, which always points to the address of the next instruction. The PC register will always know what the next step will be when the program is in progress. The value of the PC register is always undefined at the time the local method is executed. The method area method area is used to hold the original information of the class. The information used to describe the class, including the type constant pool, the field method information, and the method bytecode. In JDK6, the string constants are placed in the method area, but the JDK7 is moved to the heap. So in this respect, the method area, what is stored in the heap what information and JDK version has a great relationship. In general, our method area is the original information that holds some classes. The method area is usually associated with the permanent region (Perm), which holds some relatively stable data, and the Java heap 1.java heap should be the most closely related memory interval in the development of the program, and the objects that we have come out of in the program development are basically stored in the Java heap. 2. The heap is shared globally, and all threads share the Java heap, that is, after you have created an object, all the threads are accessible. 3. From the point of view of GC, the structure of the Java heap is related to the algorithm of GC. The Java stack 1.java stack is thread-private compared to the heap, and the stack is made up of a series of frames, so the Java stack is also called the frame stack. The content saved in the frame is a method of a local variable, an operand stack, and a constant pool pointer. Each time the method call creates a new frame and presses the stack. Let's take a look at a C + + code in the code above in the method (), we have a new object, the new object is allocated on the heap, but on the heap allocation has a problem is that each time we new object to manually delete this object, free memory. If we have allocated object space on the heap many times, but have forgotten to delete the object, there will be a memory leak, that is, we allocated space but not deleted. Memory leaks are a very difficult problem to solve in real-world development, because memory leaks can happen anywhere. We can use the right way to declare an object, we declare an object like the method on the right, then he does not actually partition the memory space, but only on the Java stack to produce a reference. This reference is automatically released after we use it and does not cause a memory leak. The allocation on the Java stack on the benefit stack is generally relatively small objects, in the absence of escape (escaping means that the object is created not only in the current thread toUse, the other thread is also to invoke the case), directly assigned to the stack. GC can be automatically recycled, reducing the pressure of GC. A large object or escape object cannot be assigned to the stack. We can interact with the above code and the main time, and a program that wants to do it requires several memory areas to be executed interactively. From the above figure we can see that each thread reads and stores the working memory of the thread. There is definitely some slack in the storage of the thread's working memory back to memory. That is, after changing the value of a variable, another object that calls the variable cannot be immediately known. If you want to make this change visible to other threads immediately, use the volatile keyword modifier. Once this keyword is used, all threads that call this variable go directly to the main memory to fetch the data. The graph below is the relationship between the thread and the local memory and main storage. Threads always take variables in their own local memory, while local memory stores only a copy of the shared variable, and the true shared variable is stored in main memory. So there is a certain delay and error between this. Visibility visibility refers to the ability for other threads to know immediately after a thread modifies a variable. The method of ensuring visibility is the three methods mentioned. Order and Order rearrangement: in one thread, all instructions, all operations are ordered. But on-line observation, in a multi-threaded situation to observe the behavior of the previous thread, we will find that the behavior may be disordered (this disorder has two reasons, one is the command rearrangement, the other is the main memory synchronization delay, that is, the thread a changed the value of a variable, synchronous main memory is also successful, However, in thread B we may not have time to synchronize the values in main memory, and at this point the operation of thread A may be unordered. Basic principles of command rearrangement: 1. Procedure order principle, which guarantees the serialization of semantics within a thread. 2.volatile rule: The write of the volatile keyword variable occurs first in the read. 3. Lock rule: Unlocking must occur before a subsequent lock is added. 4. Transitivity: A precedes b,b before C, then a must precede c. 5. The start method of a thread precedes each of its actions. 6. All operations of the thread precede the end of the thread (Thread.Join ()). 7. Thread interrupts (interrupt ()) are preceded by code that is interrupted by a thread. 8. The object's constructor execution ends with the Funlize () method.
Java Virtual Machine (i): JVM operating mechanism