Insidejvm (4) -Heap (Heap)

zhaozj2021-02-11  143

Piles a Java application All class instances or arrays created in the run are placed in the same heap and share all threads. Because a Java application is only a JVM instance, each application has a heap of each application, it is impossible to destroy the heap of another application. However, a multi-threaded application must consider synchronous problems.

JVM has instructions to allocate objects in the heap, but does not release the object's instructions. As you can't release an object with Java code, the bytecode has no corresponding function. The application itself does not have to consider when and what method is used to recycle the memory that does not use the object. Typically, JVM handed this task to the garbage collector.

The main job of garbage collection a garbage collector is to recover the memory occupied by the object that is not referenced. It may also go to move the object still used to reduce memory fragmentation.

The JVM specification does not specify what technology is used for garbage collection, which is determined by the implementation of JVM. Because there may be many places, such as Java stack, heap, method area, Native Method stack. Therefore, the use of garbage collection techniques largely affects the design of the running data area.

Like the method area, the heap does not have to be a continuous memory area, or the size can be dynamically resized according to the needs. The method area can be placed on the top of the heap. In other words, the type information and the actual object are all on the same pile. The garbage collector responsible for cleaning the object may also be responsible for recycling. The initialization size of the heap, the maximum minimum size can be specified by the user or program.

Object Reperestation (Translator: C , called Object Model) The JVM specification does not specify how objects are in the heap. The performance of the object affects the entire design of the heap and garbage collection, which is determined by the JVM implementation.

The main data of the object is composed of instance variables declared by the corresponding class and its parent class (INSTANCE VARIABLES translator: Class Variables, Class Variables is stored in the method area, this is in the upper translation) JVM should be both from one Object reference quickly finds instance variables, and it is also possible to quickly find class data stored in the method area. Therefore, there is often a pointer to the method area in the object.

One possible implementation is to divide the heap into two parts: a handle pool and a target pool. Figure 5-5 An object reference is a native pointer to the handle pool. Each entry of the handle pool has two parts: a pointer to the object instance variable, a pointer to the method area type data. The advantage of this design is to make the stack of sorting, when moving the object to reduce fragmentation, do not need to update each object reference, and only change the handle. The disadvantage is that each access object is passed twice.

Figure 5-5

Another design is to direct the object pointer directly to the object instance variable, and in the object instance contains a pointer to the method area type data. The advantages and disadvantages of such a design are just the opposite of the previous method. Figure 5-6.

Figure 5-6

JVM has several reasons to enable it to get the corresponding class data from the object reference. 1. When the application tries to transform (CAST), JVM needs to ensure that the type of rotation is that this type itself or the parent type of this type. 2. 3 when the application performs instanceOf operation. When an instance method is activated, the JVM must perform dynamic binding, and what it depends on is not the type of this reference, but the information corresponding to this object.

No matter what the object is manifested, it seems to have a methodometer that can be easily accessible. Since the method can accelerate the call of the instance method, the performance of the JVM has an important impact. The JVM specification does not specify whether you must use a party, such as in an environment where the memory is rare, may not afford the memory spending of the method table. However, if the method table is used, it should be able to get quickly from an object reference.

Figure 5-7

Figure 5-7 shows an implementation of a link method table and an object reference. The data of each object contains a pointer to a special data structure. This data structure is located in the method area, which includes two parts: one of the methodological table method table method table method table for the method area corresponding to class data. It is a pointer to method data, and method data comprises: one of the operand stacks of this method and the size of the two-way method of the local variable zone, three exception tables are sufficient JVM to activate a method. The function pointer of the method table includes a function of the class or its parent class declaration. That is, the function points to the method table may be such a statement, or it may be inherited. If you are familiar with C internal working principle, you will find that this is very similar to C VTBL. In C , the object consists of an instance data and a pointer pointing to the virtual function, and the JVM can also use this method. JVM can add a method table for each object in heap, which takes more memory than Figure 5-7, but can improve some efficiency. This solution is applicable to a systemic system. (Translator: I always feel that the author is misunderstood for C , and the design of C in the function table is similar. In the case of a virtual function (not considering more inheritance), each object is only A pointer to the vTable, while vtable is also associated with classes.)

In addition to the example data shown in Figures 5-5 and 5-6, the object data has a logical part, which is an object lock (Object's Lock). Each object in the JVM has a lock for synchronization when multi-threaded access. Only one thread has this object lock at a moment, and only this thread can access the data of the object. Other threads to access this object are only waiting until the thread ownership is released. When a thread has an object lock, you can continue to add a request. But a few times must be released several times.

Many objects may not lock in their lifetime, which does not require additional data, as shown in Figures 5-5 5-6, there is no pointer to lock data in the object data. Only when you need to lock, you need to lock data, but other methods need to contact object data and corresponding lock data, such as in a tree that locks the lock data in an object address.

In addition to data that implements locks, each Java object is logically added to data to be synchronized. The lock is used to implement mutual exclusive access to shared data, while synchronization is to achieve multiple threads to work together to complete a common goal.

Synchronization is achieved by a waiting method and notification method. Each class has inherited three waiting methods from Object (three called WAIT () overload functions) and two notification methods (notify () and notifyall ()). When a thread calls the WAIT method on an object, JVM blocks this thread and put it in the WAIT Set of this object. When a thread calls the notification method in this object, JVM will wake up one or more threads waiting for a centralized block at a certain time. Like lock data, not every object requires synchronization data. Many JVM implementations separate synchronization data from object data, only to create synchronization data for this object when needed, is typically called waiting methods or notification methods in the first call.

Finally, an object may also contain data related to garbage collection. Garbage collection must track each object, this task inevitably attaches some data, the type of data is determined by the algorithm of garbage collection. For example, if the garbage collection uses the flag clearance algorithm, you must have a data to mark whether this object is referenced. Like the thread lock, these data can be placed outside the object. Some garbage collection techniques only require additional data at runtime. For example, the flag clearance algorithm uses a bitmap to mark the reference of the object.

In addition to the reference of the logo object, the garbage collection also distinguishes whether an object calls Finalizer. Before collecting an object, the garbage collector must call the object that declares the class of Finalizer. Java language specification points out that the garbage collector can only call Finalizer for an object, allowing this object to resurrect, even if it is again referenced again. This will no longer call Finalizer when this object is collected again. There are not many objects of Finalizer, and there are fewer objects, so it is rare to reclaim two times for an object. This is logically logically a part of the object, but is usually saved separately from the object. Array Performance in Java, an array is a mature object. Like other objects, the array is also stored on the heap, and the designer implemented by the JVM also has the right to determine the performance of the array.

An array also has a related class instance (Class Instance), all of which have an array of the same dimensions and types as a class, regardless of the length of the array (the length of each dimension of the multi-dimensional array). For example, an array with three INTS and an array with six INTS is the same class. The length of the array is only related to the instance data.

The name of the array class consists of two parts, one is the type represented by the dimension and one character represented by '['. For example, the class name "[i" of the one-dimensional array of type INTS is. The three-dimensional array of type Bytes is "[[[[[[[[[[b". The type of two-dimensional array of objects is "[[ljava.lang.Object".

The multidimensional array is expressed as an array of arrays. For example, a two-dimensional array of type INTS will be represented as a one-dimensional array, and the array element is a reference to a one-dimensional INTS array. Figure 5-8

Figure 5-8

The data that must be saved for each array is the length of the array. The JVM must be able to get the length of the array from an array, access the array element, check whether the array subscript is off, and activates the Object declaration.

转载请注明原文地址:https://www.9cbs.com/read-5639.html

New Post(0)