How the Java JVM avoids memory leaks, overflows __java

Source: Internet
Author: User
Tags garbage collection

Summarize the relevant knowledge in the network and book, introduce how to avoid memory leak, overflow first introduce what is memory leak, overflow (refer to my collection):

1, Memory leakage memory leak: The object can reach but is not used; is the program after the application of memory, can not release the requested memory space, a memory leakage hazard can be ignored, but the memory leak accumulation consequences very serious, no matter how much memory, sooner or later will be taken up.

2, memory overflow out of memory: memory size is not enough; is the program in the application of memory, there is not enough memory space for its use, appear out of the memory; For example, an integer is applied, but the number saved by long to save is a memory overflow. how to avoid memory leaks, overflows 1, releasing references to unwanted objects as soon as possible
The good thing is to use a temporary variable, so that the reference variable is automatically set to null after the active domain is launched, implying that the garbage collector collects the object and prevents a memory leak. 2, the process of string processing, as far as possible to avoid using string, but should use StringBuffer.
because the string class is immutable, each string object occupies an area of memory independently. 3. Use static variables as little as possible
because static variables are global, there are method areas, and GC does not recycle. (a method area implemented with a permanent generation, garbage collection behavior is relatively rare in this area, and the main goal of the garbage collector is for constant pooling and type unloading 4, to avoid centralizing the creation of objects, especially large objects, and if possible, use flow operations
The JVM suddenly needs a lot of neicun, It's going to start. GC Optimized system memory Environment 5, use object pooling technology to improve system performance
long life-cycle objects tend to cause memory leaks when they have short life objects, such as when large collection objects have business objects with large amounts of data, they can consider chunking. Then resolve a piece of strategy to release a piece. 6, do not create objects in frequently called methods, especially taboo in the loop to create an object
You can use Hashtable,vector to create a set of object containers, and then take those objects from the container without having to discard each new one. 7, optimize the configuration What is the solution of memory overflow: 1, optimize from the code level, try to avoid the situation; 2, adjust the optimization server configuration:
1) Set-XMS,-XMX, etc.
2 set newsize, Maxnewsize equal
3) set Heap Size,permgen space

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.