Advanced Java virtual machine memory layout and content

Mind Map this article .png

java memory-related region

  • java references

  1. Similar strong reference Object o = new Object (); such references. As long as the reference is still, the garbage collector does not reclaim lost referenced object.

  2. Soft references SoftReference class only in the memory of the time before the JVM will reclaim the object in the system memory impending overflow exception, these objects will be included in the scope of the recovery. Application scenarios: java which in general you can stream for bitmap images, especially photographs lot of time.

  3. When the weak reference WeakReference class when JVM garbage collection, regardless of whether sufficient memory, the recovery will be weak references to objects associated application scenarios: If an object is occasional use, and you always will be able to get in use, but do not want to influence garbage collection for this object, you should use Weak Reference to remember this object.

4. PhantomReference class virtual reference virtual reference is created using PhantomReference reference phantom reference also called ghost or phantom cited references, it is the weakest of all a reference type. Whether an object has a virtual presence referenced, it will not hurt its life cycle can not be obtained by a phantom reference object instance.

  • Algorithm determination target is dead

  1. Reference counting drawback: If A with B, A reference to B, B references A judge will cause the object did not die.
  2. Reachability analysis algorithm
  • Memory layout of objects in the HotSpot Virtual Machine

1. The object header Header

1.1.  Mark Word

Runtime data (eg: hash code (hashCode)) stored in the object itself, the GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamps in this part of the data length is 32-bit virtual machines with 64-bit virtual machines are 32bit 64bit

1.2.  Types pointer

Its object is a pointer to the metadata, the virtual machine is determined by the pointer which is an instance of the object class. (Note that it is important: if the object is a java array, then the object must have a head for recording the data length of the array, because the virtual machine can be confirmed by ordinary java object metadata java object metadata from the size of the array, but but can not determine the size of the array)

image.png

2.  alignment padding padding

Not necessarily exist, there is no special meaning. Only the placeholder for the effect. Because HotSpot VM automatic memory management system request object actually address must be an integer multiple of 8 bytes. The head and the object happens to be a multiple of eight Accordingly, when an object instance is not part of its data, which is used to fill up the whole.

3.  Examples of Data

Real object store valid information, but also various types of field contents of program code as defined. Inherited parent class or subclass definition are to be recorded. Allocation strategy with field parameters stored in the order of virtual machines that are affected in order java source code definitions.

  • Access the location of the object

1. Handle Access

Benefits: stable storage handler address, when the object is not moving (moving object when garbage collection is very common) only changes the instance data pointer in the handle, the reference itself and does not need to be modified.

image.png

1. Direct access pointer

Benefits: The biggest advantage is fast, directly to the object instance data (object instance data re-quality town). Save a pointer to an object positioning

image.png

  • Garbage collection algorithms (methodology)

  1. Mark - sweep algorithm

Divided marked with clear phases: the object to be marked recycling collection, recycling and unity. Disadvantages: 1 efficiency mark with two clear process efficiency is not high; 2 mark after clearing, a large number of discrete memory fragmentation; 3 when allocating space debris led to a large memory object can not find enough contiguous memory space leads to advance In addition trigger another garbage collection action.

  1. Replication algorithm

General commercial virtual machine using this collection algorithm to recover the new generation. Generally divided into Eden: survivor: survivor 8: 1: 1 is generally used wherein a survivor with Eden, replication until recovery when the object to be viable survivor memory area is not used. Sometimes not guarantee 10% of the survivor enough memory, depend on other memory (years old) 3. Mark allocation guarantees - Collation Algorithm

Clearly marked with the algorithm similar, but the second step is not clear, but the move will all live objects to move to one side, then empty the other memory. 4. generational collection algorithm

The memory is divided into different pieces target according to the inventory cycle. Generally classified as: the new generation with the old era. New Sound Generation: There are a large number of dead objects, only a few survived, the choice of replication algorithm. (Pay only a small amount of live objects can be done to collect the cost of reproduction) years old: an object of high survival rates, there is no space for it to be secured outside the two. We must use the mark - finishing algorithm or mark - sweep algorithm

  • Garbage collector (realization)

Seven kinds of connections on the garbage collector can be used with each other

The new generation of collectors:

  1. Serial oldest collection of basic replication algorithm uses single-threaded collector uses a CPU or a thread to complete garbage collection. But when garbage collection, you must stop all other work threads. Suitable for use in a virtual machine Client mode

  2. Serial ParNew is an upgraded version of multi-threaded version. (Operation in the multi-CPU environment, the situation will not be better than a single CPU Serial, and relatively poor.) Belonging to the multi-threaded parallel collector. Service apply to many re-mode virtual machine of choice for the new generation of collectors (use it mainly because it can be used with use with CMS, taking into account the multi-CPU efficiency can be improved as compared Serial) (note the difference between parallel Parallel Concurrent Concurrent with the)

  3. Parallel Scavenge using the copy algorithm that is focused on the throughput: running time code / (time running code + garbage collection time) applied to the background computation tasks without too much interaction

Collector years old:

  1. Old's version Serial Old (MSC) Serial collector. Using mark - sort algorithm mainly to the virtual machine in the Client mode to use. Two purposes: 1 jdk1.5 as well as previous versions and with the Parallel Scavenge with the use of CMS 2 as back-up plan **

  2. Parallel Old Parallel Scavenge old years of this multi-threading and marking - after sorting algorithm jdk1.6 provided. Focus on throughput and CPU resources-sensitive applications can preferred the new generation of Parallel Scavenge + years old Parallel Old **

  3. CMS Concurrent Mark Sweep get the best end of the collection pause time goal collector. A large part focused on the java application server or Internet site B / S system, with particular emphasis on the response speed of such applications server. Based mark - sweep collector than the previous method is more complicated. Consists of four steps: initial re-mark mark mark concurrent concurrent Clear (three major shortcomings: 1 CPU resources sensitive; 2 can not handle floating garbage (after marking thread running continue to produce new garbage), only to deal with this GC 3 based mark - sweep method, resulting in the end there will be plenty of space debris collected, often appear years old and there are a lot of a lot of space debris, unable to find enough memory to re-trigger GC)

Another is the presence of a new generation of collectors with the old year: GI

image.png

Reference: "in-depth understanding of the Java Virtual Machine" section of online articles if infringement please contact me

Guess you like

Origin juejin.im/post/5dbeeeb9f265da4d307f14c7