Java background question finishing and solution (ii) JVM related articles

Source: Internet
Author: User

The instantiation order of the class, such as the parent class static data, constructors, fields, subclass static data,

first Static, after the father son.
Static First: parent static > Child static
Precedence: Parent > Subclass static code blocks > non-static code blocks > Constructors
constructors, fields, their order of execution

the instantiation process for a class:
1, static code block in the parent class, static for the current class
2, sequential execution of normal code blocks of the parent class
3, the constructor of the parent class
4, subclass generic code block
5, the constructor of the subclass (current class), executed sequentially.
6, the execution of the subclass method,


JVM Memory Generational

Memory generational improvements for Java 8

from permanent to meta-space, automatically extend eternity in small range to avoid overflow

JVM garbage collection mechanism, when to trigger operations such as MINORGC

Heap memory is divided into the old age of young generations and the enduring generations.
The young generation is also divided into the Eden and survivor districts.
If the two zones are full, the minor GC is the one that is triggered.


Memory recovery strategy for Cenozoic and Laosheng generations

the collection algorithm used in the Cenozoic: the replication algorithm. The idea of the algorithm is to divide the available memory into two blocks of equal size, each time using one piece, and when a piece of memory is used up, the surviving object is divided into another. The used memory space is then cleaned up once.

The collection algorithm used in the old age: marker-collation algorithm. Algorithm idea: First mark the objects that need to be recycled, then let all the surviving objects move to one end, and then directly clean out the memory outside the boundary.
Ratio allocation of Eden and survivor, etc.
in-depth analysis of Classloader, parental delegation mechanism

ClassLoader

Classload: ClassLoader (class loader) is used to load Java classes into a Java virtual machine. The Java source program (. java file) is converted to a Java byte code (. class file) after it has been compiled by the Java compiler. The ClassLoader is responsible for reading the Java byte code and converting it into an instance of the Java.lang.Class class.

Parental delegation mechanism

When a particular class loader receives a request to load a class, it first delegates the load task to the parent class loader, recursively, and returns if the parent ClassLoader can complete the class load task, only to load itself when the parent ClassLoader cannot complete the load task.




JVM Common parameters

52071473
Tomcat structure, class loader process

57404890


talk about strong references, soft references, weak references, virtual references, and relationships between them and the GC

1. Strong references

As long as the program creates an object from the New keyword, the garbage collection period never recycles the object, unless the system has enough memory, and even so, the JVM simply throws a OutOfMemory () exception, if the object's reference is freed. This object will be released. So how do you clear this object's reference? The reference to the object is stored in the JVM's stack memory, so we clear the reference in the stack to erase the memory space occupied by the object.

2. Soft reference

The GC will reclaim the object's memory space when there is not enough memory resources to overflow, thus avoiding memory overflow errors. How to implement a soft reference:

Object obj = new Object ();

softreference<object> SF = new softreference<object> (obj);

obj = null;

Sf.get (); At this time, SF is a soft reference to the Obj object, which can be obtained by the Get () method, which may return NULL when the memory is low;

Usage scenario: When a user needs to implement a caching-like function, the value can be obtained directly from a soft reference with sufficient memory, without querying the data from a busy real source, raising the speed, and when the memory is low, it automatically deletes some cached data and queries the relevant data from the real source.

3. Weak references

Recycling the second garbage collection, regardless of the current memory resources are sufficient, this is the legendary lying gun it .... How to implement a weak reference:

Object obj = new Object ();

weakreference<object> wf = new weakreference<object> ();//Create weak Reference object

obj = null;

Wf.get (); Sometimes return null when back

Wf.isenqueued (); Returns whether the garbage collector is flagged as a ready-to-recycle status
Weak references are primarily used to monitor whether objects have been flagged by the garbage collector as garbage that is about to be reclaimed, and can be returned by a weakly referenced isenqueued method to whether the object is flagged by the garbage collector.
4. Virtual references (Ghost references)
obj = null;
Pf.isenqueued ();//Returns whether the object has been cleaned
Virtual references are primarily used to detect whether an object has been removed from memory.

A virtual reference is a reference with the lowest level of discourse, and as long as garbage collection begins to work, the virtual reference is recycled. Implementation method:

Object obj = new Object ();

Phantomreference<object> PF = new phantomreference<object> (obj);

Pf.get ();//always returns null

Third, the development of actual combat

As a beginner Java development engineer, you may never consider the memory problems of the JVM, but occasionally you may still have outofmemory errors, such as deploying a larger Tomcat

Web Services in fact at this time people do not need to panic, usually we can configure the Tomcat configuration file to increase memory, or the JVM's memory settings, and we can also through the JVM's GC mechanism to a program

optimization, such as using soft references, can promote GC recovery to a certain extent, thus avoiding memory overflow situations.

Especially in the development of Android programs, due to the shortage of hardware resources, we have to learn to use these mechanisms to optimize our programs.

Java background question finishing and solution (ii) JVM related articles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.