java memory leak and memory overflow

A: Definition

  Memory leak: (Memory Leak) memory would no longer object to be used can not be recycled, is the memory leak

              Strong reference points to the object will not be recovered, it may lead to memory leaks, the virtual machine will not go OOM would rather throw reclaim the object of his points

                                                 Meaning that when you use the resources opened up a space for him to forget when you run out of free up resources, and this time was also occupied by the memory, time does not matter, but the number of memory leaks more will lead to memory overflow

  Memory Overflow: (Out Of Memory --- OOM) refers to the application program memory, there is not enough memory available for applications to use, or that gives you a storage space to store data of type int, but you have to store long data types , then the result is not enough memory,

                    At this time, the error will be OOM, the so-called memory overflow, in short, they need to use the space than we have large memory is not enough memory to use the memory caused by the overflow

  Release memory: that clean up unreachable objects, and the decision is carried out by the GC, so the GC monitors the status of each object, including application, references, referenced and assignment and so on. Release the object underlying principle is that the object will not be used

II: A memory leak occurs four field

1. Recurrent memory leak. Code memory leak to be performed multiple times, each time to be executed will lead to a memory leak.
2. episodic memory leaks. Code memory leak occurs only under certain environmental or process operation will occur. Recurrent sporadic and are opposed. For a particular environment, sporadic perhaps it becomes often made of. So the test environment and test methods to detect memory leaks is essential.
3. The one-time memory leak. Code memory leak will only be executed once, or due to defects in the algorithm, there will always be a just cause and a memory leak. For example, allocated in the constructor class memory, but did not release the memory destructor, so the memory leak occurs only once.
4. Implicit memory leak. Non-stop program allocates memory during operation, but only until the end of free memory. Strictly speaking, a memory leak did not happen here, because the release of the final program memory of all applications. But for a server program, you need to run a few days, weeks or even months, not timely release of memory can also cause all memory eventually run out of the system. So, we call this type of implicit memory leaks memory leaks.

From the perspective of the user program's point of view, the memory leak itself does not generate any harm, as a general user, do not feel the presence of a memory leak. Real harm is the accumulation of memory leaks, which will eventually consume all system memory is exhausted. From this perspective, a one-time memory leaks and no harm, because it does not accumulate, and the dangers implicit memory leaks is very large, as compared to Recurrent episodic memory leaks and it is more difficult to be detected

Three: the occurrence of memory overflow and solutions

1: memory overflow reason:
  the amount of data loaded in memory 1 is too large, too much data as extracted from a database;
  2. collection class has a reference to an object, not emptied after use, can not be recycled so that JVM;
  3. ; dead cycle or repeating cycle of excessive physical presence of the object code
  BUG 4. the third-party software used by;
  5. start the memory is too small parameter values set
2: Causes and solution memory overflow:
  1: Review JVM startup parameters, directly increase the memory. (-Xms, -Xmx parameters must not forget to add.)
  2: Check the error log, see "OutOfMemory" if there are other anomalies or errors before the error.
  3: the code walkthroughs and analysis to identify potential areas of memory overflow.
  4: use of memory view tool dynamic view memory usage  

1 a first example of) memory leak: Four

 

 

Four 2): The second example of a memory leak

 

Four 3): The third example of a memory leak

 

Four 4): The fourth example of a memory leak

 

 Five: How to prevent memory leaks

  1: special attention to some like a collection of objects HashMap, ArrayList, and they often lead to memory leaks. When they are declared as static, their life cycle and will be as long as the application

  2: Resource not cause a memory leak shut down if there is no transfer of close () method

  3: Check the database query, if there is a query to get all the data. In general, if one takes thousands of records into memory, it may cause memory overflow.

   This problem is rather hidden in the front of the line, less data in the database, not easy problems, on the line, more data in the database, a query is likely to cause memory overflow. So for ways to maximize the use database query page queries

  4: Is there an infinite loop or a recursive call to check code

  5:检查是否有大循环重复产生新对象实体

  6:检查List、MAP等集合对象是否有使用完后,未清除的问题。List、MAP等集合对象会始终存有对对象的引用,使得这些对象不能被GC回收

Guess you like

Origin www.cnblogs.com/s6-b/p/12112048.html