[Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

Time:2021-1-23

Overview of JVM

JVM is a specification for computer equipment. It is the software implementation of a fictitious computer. In short, JVM is a container for running byte code programs.

It has an interpreter component, which can realize the communication between Java bytecode and computer operating system. Java programs only need to be compiled once in the JVM and run more than once, so Java has cross platform.

Memory structure

  • Method area (constant pool, static variable, constructor, class data)
  • Heap (object, class instance, main area of GC)

    • Cenozoic area (minor GC and full GC clean up useless data)

      *Eden (object creation)
    • Old age (object declaration cycle to the end of old age) (full GC)
    • Permanent area (replaced by meta space after JDK 1.8) (major GC)
  • Program counter (recording the memory address of each running thread)
  • Virtual machine stack (each method creation creates a stack, and the data in the stack is temporary)
  • Native Method Stack
  • Direct memory

    [Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

GC recovery process in heap

Objects will be allocated and created in Eden. When Eden does not have enough space, a minor GC will be initiated. When Eden does not allocate enough space for objects after executing minor GC, large objects will directly enter the old generation. You can use parameters to set large objects directly into the old generation to avoid frequent minor GC. If the object is created in Eden, it still survives after minor GC and can be accommodated by survivor, and the age is increased by 1. When the object reaches a certain age and enters the old generation, the default value is 15. Before the occurrence of mrinor GC, check whether the maximum available continuous space of the old generation is greater than the total space of all objects of the new generation. If it is greater than, it indicates that the minor GC is safe. Otherwise, it will judge whether the guarantee fails. If the guarantee fails, it will judge whether the maximum continuous space of the old generation is greater than the average size of objects promoted to the old generation. If it is greater than, it will try minor GC Otherwise, the full GC is executed to recycle the object. If the object cannot be created after the full GC is executed, the memory overflow exception will be thrown directly( java.lang.OutOfMemoryError )。

How to change the maximum value of objects entering the old age?

By modifying the – XX: preseturesizethreshold parameter to set the age of the object entering the old age. This also avoids a lot of memory duplication between the Eden area and the two survivors. (default is 15)

How can GC judge whether an object is recycled or not

[Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

Algorithm of garbage collection

[Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

They all have garbage collectors

[Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

Memory leak (memory of objects that are no longer in use cannot be reclaimed by GC)

Examples of memory leaks:

Singleton mode:

Improper use of singleton mode is a common problem that causes memory leaks. Singleton objects will exist in the whole life cycle of the JVM (in the form of static variables) after initialization. If a singleton object holds an external reference, the object will not be recycled by the JVM normally, resulting in a memory leak. So you need to be careful not to hold large objects in a singleton.

Various connections:

For example, database connection, socket connection and file stream will not be automatically garbage collected unless they explicitly call the close () method to close the connection.

Static collection class:

We loop through the application object and put it into a vector. If we just release the reference itself, the vector still references the object, so the object is not recyclable to GC.

If an object is added to a vector, it must be deleted from the vector. The simplest way is to set the vector object to null.

Static Vector v = new Vector(10); for (int i = 0; i < 100; i++) {     Object o = new Object();     v.add(o);     o = null; }
Event listener:

The event processing mechanism of AWT is a kind of delegating event processing mode: the general component (event source) delegates the whole event processing to a specific object (event listener); when the specified event occurs in the event source, it will notify the delegating event listener, and the event listener will handle the event.

For example, the commonly used listeners are actionlistener, keylistener, mouselistener, and mousemotionlistener (specialized in handling mouse movement events, such as mouse movement and dragging)

If you don’t remember to delete these listeners when releasing objects, it will increase the chance of memory leakage.

import  java.awt .*; import  java.awt.event . *; public class testbutton {public static void main (string args)] {frame f = new frame ("test"); button B = new button ("press me!"); B. addactionlistener (New buttonhandler()); / * register event listener * / F. setlayout (New flowlayout()); / / set layout manager F.add (b); f.setsize (200100); f.setvisible (true);}} / / only when the interface actionlistener is implemented, can it be the event handler. Class buttonhandler implements actionlistener {public void actionperformed (ActionEvent E)} System.out.println ("Action occurred");   } }

tuning

[Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

JVM mind map

[Special Topic on JVM] from overview to detailed explanation, including in-depth analysis of mind map!

last

All these JVM chapters have been organized into a set of complete and systematic PDF documents. Whether it’s mind maps, study notes or interview sites, there are still a lot of actual contents, so we don’t show them one by one. If you also need this set of learning materials.

Pay attention to my official account: future prospects can be collected.