& Lt; Practice & gt; analysis of class practices loaded by Classloader stored on PermGen

Source: Internet
Author: User

Introduction:

In the above article, we used the MAT tool to analyze many details about the heap memory, and the memory in addition to the heap memory, there are non-heap memory including the stack and Method Area), while the method area PermGen, it mainly stores two types of things: one is the class loaded by ClassLoader and the other is a String constant. This article focuses on the class loaded by ClassLoader on PermGen.


Practice:


Ideas:

Because the HeapDump file is generated during OutOfMemoryError

Use command line parameters-XX: + HeapDumpOnOutOfMemoryError), And the HeapDump file will not only contain heap information, but also information about the Class loaded by ClassLoader on Perm.Practical IdeasIt is to first construct a scenario, let infinite number of ClassLoader load classes through an infinite loop, so that PermGen OOM can be made, and then analyze HeapDump with tools.



Preparation:

To do this, add the following command line options:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140104/0100095403-0.jpg "title =" 51.png" alt = "wKioOVLE1gODGgU3AAA0APyDK2Y467.jpg"/>

Here I will explain:

-XX: PermSize = 16 MAnd-XX: MaxPermSize = 32 MThe goal is to keep Perm as small as possible to shorten the lab time.

-XX: + HeapDumpOnOutOfMemoryErrorThe purpose is to generate a heapDump file during OOM, and this file can be used to analyze the information of loaded classes on Perm.

-XX:-ClassUnloadingThe purpose is to prevent HotSpot VM from performing GC for Classes loaded on PermGen during Full GC, so as to shorten the lab time (that is, to make the memory occupied by the loaded class only increase or decrease, so it is easier to OOM)

-VerboseNeedless to say, let's see the execution, so we can know that the ClassLoader is loading the class



Code:

Based on the above ideas, we wrote a program:

/*** Let infinite number of Classloader to load a Class. Because different ClassLoader loads different classes, the loaded Class will be placed in * Memory Perm, this will eventually cause memory overflow. We can set a small point in the memory Perm area and load a large class (for example, more than 1000 rows) * To shorten our experiment time * @ throws Exception */public static void makePermOutOfMemory1 () throws Exception {// use URLClassloader to load the byte code file of classes under any directory/JAR package String path = "D:/Charles/Workspace2/MemoryLeakResearch/classloaded/commons-codec-1.4.jar "; URL url = new File (path ). toURL (); URL [] urls = {url}; // create a new URLClassLoader infinitely and load the specified class while (true) {URLClassLoader cl = new URLClassLoader (urls); cl. loadClass ("org. apache. commons. codec. binary. base64 ");}}


The program is very simple, is to create countless URLClassLoader in the infinite loop, and then let them load the Base64 class in the commons-codec-1.4.jar package, this class has more than 1000 lines, is a relatively large class, therefore, OOM does not take much time.

Here, we use URLClassLoader to load classes in any directory location or JAR package. We just want to demonstrate the versatility of this Code, rather than limiting the class to this project.



Result:

After running for more than a dozen seconds, OOM happened:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140104/01000925V-1.jpg "title =" 52.png" alt = "wKioOVLE2DfgydlLAAFHECO4pog137.jpg"/>

As we imagined, PermGen overflows and generates a heap dump file.



Analysis result:

We still use the MAT tool to analyze the heap dump file"Java Basics"->"Class Loader Explorer", We see the situation of classes loaded by the class loaders in PermGen:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140104/0100095954-2.jpg "title =" 53.png" alt = "wKioJlLE2VKRD9VNAADZc-5YdAg388.jpg"/>

From this we can see that there are 2048-3 = 2045 java.net. URLClassLoader is created, which is the URLClassLoader that we create infinitely in an infinite loop. Each ClassLoader loads five classes, and the loaded classes are placed on PermGen, this causes the PermGen OutOfMemory.


Let's verify that you can open a java.net. URLClassLoader at will:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140104/01000963B-3.jpg "title =" 54.png" alt = "wKioOVLE2gDySPfmAACgPxX9Z0M938.jpg"/>

It can be found that each ClassLoader loads five classes: BinaryDecoder, BinaryEncoder, Decoder, Encoder, and Base64.

In combination with our own code, we explicitly let the newly created URLClassLoader go to the Base64 class loadClass:

URLClassLoader cl = new URLClassLoader(urls);            cl.loadClass("org.apache.commons.codec.binary.Base64");  

The Base64 class implements the BinaryDecoder and BinaryEncoder interfaces:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140104/0100095318-4.jpg "title =" 55.png" alt = "wKioJlLE2pTDYMdLAAA77NUH3SU606.jpg"/>

Therefore, the same URLClassLoader will also load BinaryDecoder and BinaryEncoder.


The BinaryDecoder interface inherits from the Decoder interface, and the BinaryEncoder interface inherits from the Encoder interface:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140104/01000aP2-5.jpg "title =" 56.png" alt = "wKioJlLE2vyD9otLAABExUX-ttA887.jpg"/>

Therefore, the same URLClassLoader will also load Decoder and Encoder.



Summary:

(1) PermGen is the "method area" in the JAVA specification. It mainly contains two parts: one is the class loaded by Classloader and the other is the string constant area.

(2) the class information loaded by the PermGen ClassLoader is stored in the heap dump file, while the String constant information is not stored in the heap dump file.

(3) by default, during Full GC, the HotSpot VM recycles memory on Heap, loaded classes on PermGen, and string constants on PermGen, however, if the vm parameter-XX:-ClassUnloading is configured, only the memory on Heap and the String constant on PermGen are recycled.

(3) For Classloader loading class sequence, it complies with the load chain mode of "parent-parent delegation.


This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1347615

Related Article

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.