What memory leak and memory overflows?

Memory leak is defined (memory leak):

A variable or an object is no longer used by the program is still in memory occupy storage space.

A memory leak does not seem to have a big impact, but the consequences of the accumulation of memory leaks is out of memory.
Memory of memory overflow OUT  :

When referring 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 type, then the result is not enough memory, then it will error OOM, the so-called memory overflow.

Relations between the two:

  1. Memory leak will eventually lead to the accumulation of memory overflow
  2. Memory Overflow what you want more than the actual memory allocation system to your space, then the system is equivalent to not meet your needs, it will be reported out of memory.
  3. Memory leak is when you allocate memory for application use (new) to the system, but did not return later ran out (delete), the results of your application to your own piece of memory can no longer visit (maybe you put it deceives address lost), and the system can not assign it to the program once again needed. The equivalent of you rented a locker with a key, after you're done with something to keep the cabinet lock, lost the key or the key did not go back, then the result is this cabinet will not supply any person to use, can not be garbage recycler recycling, because his message can not find any.
  4. Memory Overflow: a tray to use every means can only hold four fruit, you installed a five, and fell on the fell to the ground can not eat. This is the overflow. Let's say the stack, do push must be produced when a stack overflow space is full, the overflow call, do de-stacked stack overflow space-time is also the creation of space, called underflow. Is allocated memory is not sufficient to lay down a sequence of data items, called memory overflow. It means I can not afford so much, then I error,

       Since the introduction of the java JVM garbage collection, the garbage collector will automatically reclaim unused objects, understand the JVM recovery mechanisms are aware JVM is using a reference counting method and reachability analysis algorithm to determine whether an object is no longer in use objects, essentially to determine whether an object is still quoted. So for this case, due to the different implementations of the code will be a variety of memory leaks (JVM mistake to let the object is still cited this reason, you can not recover, resulting in memory leaks).

 

5 memory leak in common scenarios:

1, static collections , such as HashMap, LinkedList like. If the container is static, so they are consistent with the program's life cycle, the objects in the container will not be released before the end of the program, resulting in a memory leak. In simple terms, the long life cycle of an object held by the short life cycle of an object reference, despite the short life cycle of an object is no longer used, but because of the long lifetime of an object held by its reference leads can not be recovered.

2, the various connections , such as database connection, network connection and IO connections. In the course of the operation of the database, you first need to establish a connection to the database, when no longer in use, you need to call the close method to release the connection to the database. Only after the connection is closed, the garbage collector will recover the corresponding object. Otherwise, if in the process of accessing the database, for Connection, Statement, or ResultSet not explicitly closed, will result in a large number of objects it can not be recovered, causing a memory leak.

3, variable unreasonable scope. In general, the scope of the definition of a variable is greater than its range, is likely to cause a memory leak. On the other hand, if there is no time to put an object set to null, it is likely to lead to memory leaks.

 

public  class UsingRandom {
     Private String msg;
     public  void ReceiveMsg () { 
        readFromNet (); // receiving stored data from the network to the msg 
        saveDB (); // save the database msg 
    } 
}

As the pseudo-code above, the method by readFromNet received message msg stored in the variable, and then call saveDB msg method saves the contents of the database, this time msg been useless, since the life cycle of the life of the object msg the same period, this time msg not recovered, thus causing the memory leak.

In fact this msg variable can be placed inside receiveMsg method, when the method used up, then the msg's life cycle is over, this time can be recovered. Another method, after using msg, the msg set to null, so that the garbage collector will be recovered msg memory space.

4, inner classes held outside of class, if the object is an instance of the outer class method returns an instance of an object of an inner class, the class object inside long been cited, even if the outer class instance of the object is no longer used, but due to internal holders of class instance object of the outer class, the external object will not be garbage collection, which can also cause memory leaks.

5, the hash value change, when the object is stored into a collection HashSet later, those involved in the field can not be modified to calculate the hash value of the object, or else the object is modified after the hash value is stored into the initial set HashSet when the hash value is different, in this case, the method using the contains the object as a parameter to a reference current set of search target HashSet, it will also return a result even if the object can not be found, which also results in HashSet collection can not be deleted from the current object alone, resulting in a memory leak

 

Explained in more detail with reference to :

http://blog.csdn.net/anxpp/article/details/51325838

Guess you like

Origin www.cnblogs.com/treasury/p/12600013.html