Java Virtual machine Learning (iii) memory overflow exception

Source: Internet
Author: User

    1. Java Heap Overflow:

      When testing in eclipse, you can set virtual machine parameters in Debug/run, such as -xmx 20M represents the maximum VM heap memory size is 20M,-xms is the smallest heap memory . And then write a dead loop. The class constantly adds objects to the list collection, and when the heap memory exceeds 20M, the outofmemory exception is reported.

    2. Virtual machine stack and local method stack overflow:

      In a thread, if the depth of the method exceeds the allowed depth of the virtual machine, the StackOverflow exception is reported, such as a recursive call method. This exception is easy to implement, and the other is that the Java Virtual machine stack will be reported outofmemory exception if it cannot request memory when it is extended.

public class Main {private static int count=0;public static void Main (string[] args) {//TODO auto-generated method stubtr Y{deap ();} catch (Exception ex) {ex.printstacktrace ();}} private static void Deap () {count++; System.out.println (count);d EAP ();}}

When setting-XSS to 1M and 10M, the output of the count variable will be about 10 times times worse, I understand that the virtual machine stack runtime is actually the Java method when executing the memory model, in the endless recursion will be pushed into the virtual machine stack in the stack frame to store the execution of the operand, local variable method exports and other information. When this block of memory is full, it will report a stack overflow.

The system in the operating system limits the memory limit for each process, and if the maximum memory size for each process is set at 1GB, then in the virtual machine, the method area memory and heap memory can be set by the virtual machine parameters (Xmx and maxpersize), the virtual machine process memory minus the heap memory and the total memory of the method area. If the memory consumed by the counter and the virtual machine itself is ignored (these two parts account for a small amount of memory), the remaining memory is largely partitioned by the virtual machine stack and the local method stack. The larger the amount of virtual machine stack that each thread allocates, the fewer threads you can create.

3. method area and run-time-constant pool overflow:

The method area memory size settings can be set by the virtual machine parameters -xx:permsize and -xx:maxpermsize , and the following code in the Java 1.6 environment can implement the method area constant pool memory overflow:

public static void Main (string[] args) {//TODO auto-generated method Stubint i=0; Arraylist<string> list=new arraylist<string> (); while (true) {List.add (string.valueof (i++). Intern ());}}

The function of the String.intern () method here is that if there is a string in the string constant pool that is equal to this string object, the reference is returned, otherwise the string is stored in a constant pool, and the constant pool in Java 1.6 is allocated in the permanent generation memory, and the GC is hardly recycled. So the constant pool memory overflows (1.7 of the virtual machines have removed the string constant pool from the permanent generation), and the Fill method area memory can use Gclib to create a large number of classes, because the method area is used to hold information about class classes such as Class name, access modifier, constant pool, etc., Spring, The mainstream framework such as OSGi will encounter such problems, such frameworks need to load a large number of classes, and the recovery of a class to determine the condition is more stringent, in the method area memory allocation hours will be reported outofmemoryerror:pergen space anomaly, that is, the method area overflow.

4. Native Memory overflow:

Directmemory capacity can be set by setting the virtual machine parameter-xx:maxdirectmemorysize, experimental method: Set the direct memory limit, and constantly through the unsafe class of Allocatememory () method to apply for memory, When the upper limit is reached, the outofmemory exception is reported.

Reference connection:

http://lavasoft.blog.51cto.com/62575/25492/

Java Virtual machine Learning (iii) memory overflow exception

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.