Memory leak and memory overflow in JAVA

1. Why do you need to understand memory leaks and memory overflows?

1. Memory leaks are generally caused by errors in the code written by programmers. Understanding memory leaks can avoid unnecessary memory leaks and allow programmers to improve their coding skills.

2. By understanding the common situations of memory overflow, the problem can be found quickly and accurately, and the time to solve the problem can be shortened.

Second, the concept distinction between memory leak and memory overflow

1. Memory leak means that the program dynamically allocates memory to some temporary objects, but these temporary objects are not reclaimed by GC, and always occupy memory, even if they are useless, they always occupy memory, which is a memory leak.

2. Memory overflow refers to errors caused by not being allocated enough memory.

 

Note: It appears that memory leaks are one, not the only, cause of memory overflow.

 

Three, several scenarios of memory leaks:

1. Objects with a long life cycle are stored in objects with a short life cycle

This is one of the most common memory leak scenarios. For example, local variables are stored in the global static map without clearing operations. Over time, the map becomes larger and larger, thus causing memory leaks.

 

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

After the object is stored in the HashSet collection, the field for calculating the hash value cannot be modified, otherwise the modified hash value will be different from the originally stored hash value. In such a case, the object cannot be found even if the current reference to the object is used as a parameter in the contains method. This makes it impossible to delete this object in the HashSet collection, thus causing a memory leak.

 

3. The number of connections and shutdown time settings of the machine

Opening very resource-intensive connections for long periods of time can also cause memory leaks.

Four, several situations of memory overflow:

1. Method area memory overflow (outOfMemoryError: permgem space)

    In the jvm specification, the method area mainly stores class information, constants, static variables, etc.

 

  Therefore, if the program loads too many classes, or uses such dynamic proxy generation techniques as reflection, gclib , etc., it may cause memory overflow in this area. Generally, the error message when memory overflow occurs in this area is:

 

      outOfMemoryError:permgem space

2. Thread stack overflow (java.lang.StackOverflowError)

       The thread stack is a unique memory structure of the thread, so the problem with the thread stack must be an error generated when a thread is running.

    Generally, thread stack overflow is caused by too deep recursion or too many method call levels .

    The error message of stack overflow is:

      java.lang.StackOverflowError

Five, several methods to avoid memory leaks and memory overflows:

1. Release references to useless objects as soon as possible

2. Use string processing and avoid using String. StringBuffer should be used in large quantities. Each String object must occupy an area of ​​memory independently.

3. Use static variables as little as possible, because static variables are stored in the method area, and the method area will not be garbage collected.

4. Avoid creating objects in loops

5. Opening large files or taking too much data from the database at one time can easily cause memory overflow. Therefore, in these places, it is necessary to roughly calculate the maximum amount of data, and set the required minimum and maximum memory space values. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112222&siteId=291194637