Java from oblivion to the entry --Day02


Memory Analysis Section

Java virtual machine memory can be divided into three areas: the stack, heap, the method area.

Stack features
  1. Memory model is the method of execution stack described. Each method is called a stack frame is created (stored local variables, operands, the method exports)
  2. JVM creates a stack for each thread, the thread information is stored for performing the method (actual parameters, local variables, etc.)
  3. Stack belong thread private, shared between threads can not be achieved!
  4. Storage characteristics of the stack is the "last out, LIFO"
  5. The stack is automatically assigned by the system, fast! Stack is a contiguous memory space!
Heap of characteristics
  1. Heap is used to store the created objects and arrays (arrays are objects)
  2. Only one JVM heap, shared by all threads
  3. Heap is a non-contiguous memory space allocation flexible, slow!
Methods district features
  1. JVM is only one method area is shared by all threads!
  2. The actual method is heap area, only for storage class constants related information!
  3. Used to store the contents of the program will always be unchanged or only containsClass informationClass object [],Static variablesStatic methodString constantWait)

Very good links: run the program is explained .


Garbage Collection articles

Memory Management
  • Assigned space: you can create an object using the new keyword
  • Release object space: objects can be assigned null. Garbage collector reclaim the memory space will be responsible for all the "unreachable" objects.
Garbage recycling concept

Any garbage collection algorithms generally do two basic things:

  1. Useless objects found
  2. Recovery of memory space occupied by the object useless.

Useless object refers to the object without any variable references. Java's garbage collector found useless objects through the relevant algorithm, and removal and finishing.

Related to garbage collection algorithm

1. Reference counting
heap each object has a reference count. Is referenced once, count is incremented, the reference variable value becomes null, the count is decremented until the count is 0, then becomes useless object. The advantage is a simple algorithm, the disadvantage is "useless circular reference object" do not recognize, such as:

public class Student {
    String name;
    Student friend;
     
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
         
        s1.friend = s2;
        s2.friend = s1;        
        s1 = null;
        s2 = null;
    }
}

s1 and s2 reference each other, they cause the reference count is not zero, but has actually been useless, but can not be recognized.

2. References up method (root search algorithm)
after all the procedures to be seen as a reference to the relationship between a map, from a node GC ROOT start, find the corresponding reference node, find the node, continue to look for this reference node, and when after all the reference nodes looking for is completed, the remaining nodes is considered not to be a reference to the node, the node that is useless.

3. generational garbage collection
Generational garbage collection mechanism is based on the fact: the life cycle of different objects is not the same. Therefore, objects of different life cycle can take different recovery algorithms, in order to improve the recovery efficiency. We were divided into three states: the young generation, old generation, lasting generations. The JVM heap memory is divided into Eden, Survivor and Tenured / Old space.

(1) the young generation
all newly generated objects first of all on the Eden area. The young generation's goal is to collect as quick off the short life cycle of those objects, corresponding toMinor GCEach Minor GC will clean up the young generation of memory, the algorithm uses a high efficiencyReplication algorithmFrequent operations, but will be a waste of memory space. When the "younger generation" storage area full of objects, the object will be stored in the old generation area.

(2) the old generation
experience in the young generation in the N (default 15) after the second garbage collection objects are still alive, it will be placed in the tenured generation. Therefore, it is considered the old generation are stored in some of the longer life cycle of the object. More and more old generation object, we need to startMajor GC and Full GCA comprehensive clean-up the young generation and the old generation regional area.

(3) permanent generation
for storing static files, such as Java classes, methods and the like. There was no significant lasting impact on the generation of garbage collection.

GC distinguish three kinds:

  • Minor GC: for the young generation to clean up the area.
    Eden area is full it will trigger a Minor GC. Clean up useless objects, copy live objects to "Survivor1", "Survivor2" zones (two zones, the size of the space is the same, the same time Survivor1 and Survivor2 only one with an empty)
  • Major GC: used to clean up old's region.
  • Full GC: used to clean up the young generation, old generation area. Higher costs, have an impact on system performance.
Garbage collection process
  1. Newly created object, in the vast majority will be stored in Eden;
  2. When Eden is full (a certain percentage) can not create a new object, triggering garbage collection (GC), will clean out useless objects, and then copy the remaining objects to a Survivor, as S1, while emptying Eden District;
  3. When the Eden area is full again, the S1 of the object can not be emptied into another storage Survivor, as S2, while the area not empty Eden object, also copied to S1, and S1 to ensure Eden, are empty;
  4. Repeat several times (default 15) Survivor is not been cleaned objects will be copied to the old's Old zone;
  5. When the Old area is full, it will trigger a once complete garbage collection (FullGC), before the new generation of garbage collection called (minorGC)

Memory Leak articles

Full GC

In the process of tuning the JVM, a large part of the work is adjusted to the Full GC. The following reasons may lead to Full GC:

  1. Old generation (a position of tenured) are full;
  2. Permanent generation (Perm) are full;
  3. System.gc () is an explicit call (GC recommended program starts, instead of calling GC), as little as possible, will apply starting Full GC, high costs and affect system performance;
  4. The last time the dynamic allocation strategy changes each domain Heap after GC.
Operation likely to cause a memory leak
  • Create a lot of useless objects. For example, when we need a lot of string concatenation, we use a String instead of StringBuilder.
  • Using a static set of classes. Like memory leaks HashMap, Vector, List, etc. using the most prone to the same life cycle and application of these static variables, all objects Object can not be released.
  • Various connection objects (IO stream object, the object database connection, a network connection object) is not closed.
  • Use listener. When the object is released, do not delete the listener.

Articles object creation process

Object creation

Constructor is an important way to create Java objects through the new keyword call the constructor, the constructor also does return an object of the class, but the object is not the sole responsibility of the constructor creates. Create an object divided into the following four steps:

  1. Assigned space and object member variable is initialized to 0 or empty
  2. Performing initialization display attribute value
  3. Constructor is executed
  4. The return address of an object to the relevant variables
this keyword
  • The essence of this is to "create a good target address"!
  • This may also be used for "current object", i.e. the object about to initialization in the constructor, and must be located on a configuration method.
  • this is not a static process.

Retroactive inheritance tree

Attribute / Method search order

For example: Find a variable h

  1. Find the current class has no property h
  2. Each parent in turn be traced back to see if h each parent until Object
  3. If not found, a compile error occurs.
  4. The above steps, just find h variable, then this process is terminated.
Called by the constructor order

The first sentence is always constructor: super (...) to call the parent class constructor corresponding. Therefore, the flow is: Xianxiang traced back to the Object, then turn down the block and the initialization class constructor method performed until the current date subclass.

NOTE: static initialization block call sequence, with the same calling sequence constructor, not repeated.


Polymorphism

important point:

  1. Polymorphism ismethodPolymorphic polymorphic not attribute (attribute independent and polymorphism).
  2. There are multiple states have three necessary conditions:inheritMethod overridesReferences to parent child class object
  3. Compile look left, look to the right to run.
  4. References to parent subclass object, the process for the upward transition, are automatic type conversion. After the transition up the parent class reference variables can only call itCompilation typeThe method, which can not be called runtime type of method.
  5. If the unique method of operation types need to call on the need for mandatory conversion type, called a downcast.
  6. In the downward transition process, the reference variable must be transformed into real subclass type (run-time type), or at run-time type conversion will appear abnormal ClassCastException.
  7. To avoid this exception, you can useinstanceofOperator to judge, and then turn strong.
Published 33 original articles · won praise 9 · views 8690

Guess you like

Origin blog.csdn.net/Serena0814/article/details/104820046