Java memory Area/memory overflow rollup

Source: Internet
Author: User

This article introduces a summary of memory areas and various memory overflow scenarios in Java virtual machines.

Data region

Method area, heap, virtual machine stack, program counter, local method stack

Method area

Used to store class information, constants, static variables, and immediate compiler-compiled code that have been loaded by the virtual machine
Run a constant pool: contains literal and symbolic references generated during compilation

Exception (Outofmemoryerror:permgen space) Cause:
1. Add a large amount of data to the usual pool. Eg:String.intern ()
2. Large amount of class information (or dynamic proxy)

Exception handling:
Appropriate increase in configuration:-xx:permsize-xx:maxpermsize


Heap

Storing object instances

Exception (OutOfMemoryError) causes:
1. Memory leaks, which in the end cause memory overflow
2. Code creating too many objects results in exceeding the set memory size

Exception Handling (ref.: http://blog.csdn.net/wisgood/article/details/16818243)
1. Release a reference to a useless object as early as possible (a good idea is to use a temporary variable to automatically set the reference variable to null after exiting the active domain, implying that the garbage collector collects the object to prevent a memory leak.) )
2. When the program is string-processing, try to avoid using string, instead use StringBuffer (because each string object occupies a single area of memory)
3. Minimize the use of static variables (because static variables are global, GC is not recycled)
4. Avoid concentrating on creating objects, especially large objects, and use flow operations if possible
5. Use object pooling techniques to improve system performance
6. Do not create objects in frequently invoked methods, especially when creating objects in loops
7. Optimized configuration:-XMS-XMX

1. Is there a memory leak in the Q:java?    A:  There is also a memory leak in Java. This is caused when the allocated object is up to date and is useless (no null for the reference to the deprecated data Memory unit).         such as: Java code vector v=new vector (ten);    for (int i=1;i<100; i) {        Object O=new object ();        V.add (o);        o=null;    }    At this point, all object objects are not freed because the variable v refers to these objects.    after the object is added to the vector, it must also be removed from the vector, and the simplest way to release it is to set the vector object to null.   2. Q: What are the similarities and differences between memory leaks and overflows?     A: The same: can cause problems with the application running, degraded performance, or hangs.    B:    1) memory leaks are one of the causes of memory overflow, and memory leaks accumulate to cause memory overflow.    2) memory leaks can be avoided by perfecting the code, and memory overflows can be adjusted to reduce the frequency of occurrence, but cannot be completely avoided.

Virtual Machine Stack

Store basic data types, object references, operand stacks, dynamic links, method exits

Exception Reason:
(stackoverflowerror) The stack depth requested by the thread is greater than the maximum allowable depth of the virtual junction (eg: recursion does not bounce)
(OutOfMemoryError) Unable to request sufficient memory space on the scale stack

Exception handling:
1. Check if there are no normal end codes for recursion
2. Adjust the stack memory size appropriately-XSS


Program counter

The line number indicator of the bytecode executed by the current thread. (Control the execution order of the Code)

is a small amount of memory space, there is no provision for any outofmemoryerror


Local method Stack

Similar to the virtual machine method stack, just responsible for the native method

Hotspot adds this stack to the virtual machine stack.

Exception with virtual machine stack exception.
-xoss modifies the local method stack memory (but the hotspot parameter is not valid and is modified by setting-XSS)


Native Direct Memory

is not a memory area in the virtual machine specification. NiO and other processing directly used memory, you can use the native library to directly allocate out-of-heap memory, to avoid the Java heap and the native heap to replicate data back and forth,

Abnormal phenomenon:
After outofmemoryerror occurs, there is no obvious exception in the Heap Dump file, and if you use NIO directly or indirectly in your program, you can consider checking for this memory overflow.

Exception handling:
-xx:maxdirectmemorysize

Java memory Area/memory overflow rollup

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.