Java Interview 01| JVM-related

Source: Internet
Author: User
Tags xms

1. JVM Memory view and analysis, write memory leak instance

Heap area, stack area, method area, native memory may be memory overflow. Write a heap memory overflow instance here. Here's a book from the deep understanding of Java virtual machines.

-xms20m-xmx20m-xx:+heapdumponoutofmemoryerrorpublic class Heapoutofmemoryerror {static Class Oomobject{}public static void Main (string[] args) {list<oomobject> List = new arraylist<oomobject> (); while (true) {List.add ( New Oomobject ());}}}

Write an overflow instance to know two things:

(1) Constant creation of instance objects to fill up the heap space

(2) Guaranteed GC roots to the object can be reached between the path to avoid garbage collection

The results of the last run are as follows.

Dump the snapshot and view the reference chain of the leaked object and the leaked object to the GC roots. Install Eclipse Memory Analyzer to the Eclipse IDE for a snapshot view of the memories.

Plug-in installation address: http://www.eclipse.org/mat/downloads.php

Use the jstat command to view the allocation and usage of the JVM memory as follows:

JSTAT-GC 24170 (Process number) 1000 (1000ms acquisition of data) 2 (two data acquisition)

The results are as follows:


S0c:s0 area capacity (same as S1 area) S0u:s0 area has been used
Ec:e area capacity Eu:e area has been used
OC: Old age capacity OU: old age used
MC: Meta data capacity MU: Metadata area used
Ygc:young GC (Minor GC) times Ygct:young GC Total time fgc:full GC times fgct:full GC Total time GCT:GC Total time spent

You can also use the Java VisualVM graphical interface for viewing. You can typically install a visual GC plug-in, as shown below.

Describe the parameters as above.

1, Compile Time (Compilation): 855compiles represents the total number of compilations, 6.606s represents the cumulative time of compilation. A pulse represents a JIT compilation, a narrow pulse indicates a short duration, and a wide pulse indicates a long duration.

2. Class Loader Time: 20869loaded indicates the number of classes loaded, 139 unloaded represents the number of classes unloaded, and 40.630s indicates the time it takes to load the class

3. GC Time (GC time): 2392collections represents the total number of garbage collection times, 37.454s indicates the amount of garbage collected, and last cause indicates the reason for the recent garbage collection

4. Eden Space (Eden Zone): 31.500M in parentheses indicates maximum capacity, 9.750M indicates current capacity, the following 4.362M indicates current usage, 2313collections indicates garbage collection times, 8.458s indicates garbage collection takes time

5, Survivor 0/survivor 1 (S0 and S1 Zone): 3.938M in parentheses means maximum capacity, 1.188M represents current capacity, and subsequent values are current usage

6, Old Gen (older): 170.500M in parentheses for maximum capacity, 67.500M for current capacity, followed by 31.825 for current usage, 3collections for garbage collection, 450.789s for garbage collection time spent

7, Perm Gen (permanent): 96.000M in parentheses represents the maximum capacity, 70.250M represents the current capacity, and the following 35.129M indicates the current usage

2. Allocation of objects and conditions for departure minor GC and full GC

The object takes precedence over Eden allocations, and when the Eden Zone does not have enough space to allocate, the virtual machine will initiate a minor GC.

Before the minor GC occurs, the JDK7 rule is that minor GC will occur as long as the continuous space in the old age is greater than the total size of the new generation object or the average size of successive promotions, otherwise full GC will occur.

3, class loading process, how to get the current object ClassLoader

The process of class loading is divided into: load, connect, initialize, use, and unload. Where connectivity can be divided into: verification, preparation and resolution

Gets the classloader of the current object through the following code:

This.getclass (). getClassLoader ()  //Gets the class object of the current object, and then calls getClassLoader

Class Loaders can perform class-level partitioning, OSGi, hot deployment, and code encryption. So how do users customize the ClassLoader?

To create a user's own classloader, simply extend the Java.lang.ClassLoader class and overwrite its findclass (String name) method, which specifies the name of the class based on the parameter and returns a reference to the corresponding class object.

Findclass protected class<?> Findclass (String name)   throws ClassNotFoundException

Finds a class using the specified binary name. This method should be overridden by the implementation of the ClassLoader, which loads the class according to the delegate model. This method is called by the LoadClass method after the requested class is checked by the parent class loader. The default implementation throws a ClassNotFoundException.

4, the first sex happens-before java

The JMM (Java memory model) defines a partial-order relationship for all program internal actions, called Happens-before. To ensure that the thread executing action B sees the result of action a (whether A and b occur in the same thread), the happens-before relationship must be met between A and B.

Refer to "in-depth understanding of Java Virtual Machines" page 376

5. Class loader for JVM

(1) Which classes are loaded by each class loader

(2) How to customize your own ClassLoader, and how your own ClassLoader and Java-loaded classloader relationships are handled

namespace relationships for different classloader:

Classes within the same namespace are visible to each other.

The namespace of the child loader contains all of the parent loader's namespaces. So the class that the child loader loads can see the class that the parent loader loads. For example, classes loaded by the system ClassLoader can see classes loaded by the root ClassLoader.

Classes loaded by the parent loader cannot see the class that the child loader loads.

If there is no direct or indirect parent-child relationship between the two loaders, the classes they load are not visible to each other.

Reference:

Java Programmer's Way of cultivation, page 110th, look at the ClassLoader in dependency injection

6. Garbage Collection Strategy

Specifically, G1 's recycling strategy:

Region divides the memory space and has the priority area recovery way, guaranteed that the G1 collector can obtain the highest collection efficiency in the limited time.

7. JVM Parameters

Parameters can be broadly divided into: heap settings, collector settings, and garbage collection statistics

Parameter name Meaning
-xms Initial heap size, default is physical memory 1/64 (<1GB) When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx.
-xmx Maximum heap size, default to physical memory (<1GB) When the default free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of-XMS
-xmn Young generation size (1.4 or Lator) Note: The size here is (eden+ 2 survivor space). It is different from the new Gen shown in Jmap-heap.
The entire heap size = younger generation size + old generation size + persistent generation size.
Increasing the younger generation will reduce the size of older generations. This value has a large impact on system performance, and Sun is the official recommended configuration for the entire heap of 3/8
-xx:newsize Set the young generation size (for 1.3/1.4)
-xx:maxnewsize Young generation Max (for 1.3/1.4)
-xss stack size for each thread Generally small applications, if the stack is not very deep, should be 128k enough for large applications recommended using 256k. This option has a large impact on performance and requires rigorous testing.

-xx:newratio Ratio of young generations (including Eden and two survivor districts) to older generations (excluding persistent generations) -xx:newratio=4 says the ratio of young generations to older generations is 1:4, and the younger generation accounts for 1/5 of the entire stack.
Xms=xmx and the xmn is set, this parameter does not need to be set.
-xx:survivorratio The ratio of the Eden area to the survivor area Set to 8, the ratio of two survivor to one Eden area is 2:8, and a survivor area represents 1/10 of the entire young generation.
-xx:heapdumponoutofmemoryerror This is a very good way to diagnose the dump heap to file when oom occurs.
-xx:+printgcdetails Print GC Details
-xx:+useparallelgc Setting up a parallel collector

 

Java Interview 01| JVM-related

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.