Android will generally lead to a memory leak under what circumstances

 

Information Reference: https://blog.csdn.net/u011479990/article/details/78480091  

The reason is that the memory leak long life cycle of an object held by the short life cycle of an object reference


 

Memory leak vivid metaphor is "operating system available to all storage processes are being drained a process", the end result is a longer running time, taking up more and more storage space, and ultimately run out of storage space for all, the whole system crashes. Therefore, the "memory leaks" from the perspective of the operating system. Here does not refer to physical memory storage space, but refers to the size of virtual memory, virtual memory size depends on the size of disk swap setting. By the application of a program memory, if there is no a pointer to it, then it is this memory leak

That is  allocated heap memory for some reason the program does not release or not release

Original link: https: //blog.csdn.net/qq_29699799/article/details/80173110


 

  1. Global collections strong reference memory leaks (especially modified static set of static View view) caused, modified static variable, its life cycle is very long. static solutions: 1) should try to avoid static member variables cited instances of excessive consumption of resources. Such as Context. 2) Context try to use ApplicationContext, because the Context of Application life cycle is longer, it will not be a problem cited House leaks. 3) instead of using WeakReference context, such as may be used WeakReference mContextRef.
  2. Receivers, listeners did not cancel their registration at the time of registration will pass a reference to activity caused by memory leaks, such as broadcasting, eventsbus past
  3. Cursor cursor is not closed, io did not close the stream runs out, a variety of other connections
  4. Anonymous inner class / non-static inner classes and asynchronous thread holds the short life cycle of a reference memory leaks caused by, for example, you hold a reference to the outer class within the class, as long as the instance of the inner class has been alive, this reference will not be recovered / AsyncTask not automatically destroyed after the completion of the implementation can be used instead of RXJAVA
  5. In use some large objects such as Bitmap ah what, remember to recycle

Guess you like

Origin www.cnblogs.com/yjpjy/p/11388897.html