Memory leaks and memory overflow in Java

Source: Internet
Author: User

first, why to understand memory leaks and memory overflow

1. Memory leaks are typically caused by code errors written by programmers, and understanding memory leaks can avoid unnecessary memory leaks and allow programmers to improve their coding levels.

2. By understanding memory overflow common situations, you can quickly and accurately find the problem, shorten the time to solve the problem.

two, memory leaks and memory overflow concept distinction

1. Memory leak, means that the program dynamically allocates memory to some temporary objects, but these temporary objects are not collected by GC, always accounted for memory, even if it is useless and always occupy the memory, this is a memory leak.

2. Memory overflow, refers to the error that is caused by not being allocated enough memory.

Note: memory leaks appear to be a cause of memory overflow, not the only cause.

Three, memory leaks of several scenarios:

1. Objects with short life cycles are stored in objects with long lifecycles

This is the most common in a memory leak scenario, where, for example, a local variable is not emptied in a global static map, and as time goes by, the map becomes larger, resulting in a memory leak.

2. Modified the field in HashSet to calculate the hash value of the object

When an object is stored in a HashSet collection, it is not possible to modify the field that computes the hash value, otherwise the modified hash value will be different from the originally deposited hash. In such a case, the object cannot be found even if the current reference to the object is used in the contains method as a parameter. This prevents the object from being deleted in the HashSet collection, causing a memory leak.

3. Number of connections and shutdown time settings for the machine

Long hours of resource-intensive connections can also cause memory leaks.

Four, memory overflow of several situations:

1. Method area Memory Overflow (outofmemoryerror:permgem space)

in the JVM specification, the method area mainly holds the class information, the constant, the static variable and so on.

So if a program loads too many classes, or uses such dynamic proxy generation techniques as reflection, gclib, and so on, it can cause a memory overflow in that area, which is generally the error message when a memory overflow occurs in this area:

Outofmemoryerror:permgem Space

2. Thread Stack Overflow (java.lang.StackOverflowError)

Thread stacks a piece of memory structure that is unique to threads, so a thread stack problem must be an error that occurs when a thread is running.

Generic thread stack Overflow is caused by too much recursion or too many levels of method calls .

The error message that the stack overflow occurred is:

Java.lang.StackOverflowError

v. Several ways to avoid memory leaks and memory overflows:

1. Release references to useless objects as early as possible

2, use string processing, avoid using string, should use a lot of stringbuffer, each string object has to occupy a piece of memory area

3, minimize the use of static variables, because static variables are stored in the method area, the method area is basically not garbage collection

4. Avoid creating objects in loops

5. Opening large files or taking too much data from a database can easily cause memory overflow, so be sure to calculate the maximum amount of data in these places, and set the minimum and maximum memory space values required.

Memory leaks and memory overflow in Java

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.