Questions tagged [memory-leaks]

A memory leak occurs when a program fails to release memory that it has allocated but is no longer using and is not recoverable.


A memory leak occurs when a program fails to release memory that it has allocated but is no longer using. In many cases, the memory cannot be referenced and will therefore never be recovered to be reallocated. Even in cases where the memory can be referenced, some other code may be causing it from being recovered. Memory leaks are particularly important for developers to be wary of, as they can be extremely difficult to debug. There are many established practices in code are put into place specifically for the purpose of avoiding memory leaks.

The Impact of Memory Leaks

Every running program or application, no matter the environment must have memory allocated to itself in order to (at least temporarily) store its data and even run its code. When an program with one or more memory leaks is run, this memory will be overlooked by the system because the system thinks that it is being used, either because it has been told or the requirements for dead memory have not been met. Depending on the intensity and veracity of the leak, this can potentially quickly result in the system being unable to allocate memory for other programs (even itself) causing poor performance and unintended faults. In some less modern environments, memory leaks have the capability to crash the system unexpectedly which can lead to other issues.

Managed-Memory Environments (Intentionally general)

A managed memory environment is one where a subsidiary environment is created to directly monitor memory usage, its allocation, recovery and reallocation to programs that run within it. For many developers, this is desirable because it allows developers to build their applications and trust that the proper amount of memory will be allocated when needed and released when not. The most common of these is the Run-time Environment, which allows (and sometimes forces) programs to run in its memory space. In reality, the memory manager is just another program that runs on behalf of the system, often called the Garbage Collector.

Garbage Collectors can be system-specific or even language-specific, requiring that programs written in a particular language always run in a managed-memory space. Due to the fact that the memory manager is a program, it looks for indicators of unused memory to determine whether recovery is needed. Each memory manager has specific criteria that it checks, the most common of which is when neither the program nor any of its code reference (via a pointer) a specific block of memory. Without regard to the specific environment semantics, the most general practice for leak prevention is: Remove any reference that is no longer in use. This means to either a) release all pointers or b) set explicit object references to null (depending on the language).

Unmanaged-Memory Environments (Intentionally general)

An unmanaged-memory environment is when a program directly requests memory from the system and is responsible for releasing the memory itself. The most widely-known example of this native code in C or C++. As a result of the popularity of these languages, malloc() has become a key indicator of running in an unmanaged memory environment.

Unmanaged Memory Environments are desirable for many developers, because the program has direct control over the use of its memory space. The developer can preempt data intensive operations and request memory blocks before they are immediately needed, easing some processor strain. Additionally, they typically have a more intimate knowledge of how the memory is being used by their program and can release memory with relative certainty that it is unused.

While, there are many practices established dedicated to the avoidance of leaks, the credo is always the same: Watch what you're using, clean up what you are not. In practice, this simply means for every allocation the program makes, ensure there is at least a release of the same size before the program exits.

Avoiding Memory Leaks

Most memory leaks are caused by code practices or techniques that are not complementary to the mechanisms by which the environment allocates and frees memory. As software technology has progressed, the term environment has become more fluid as many languages have their own mechanisms to assist the system in managing memory. Even though there may be semantics that are environment-specific, most leak prevention is based on simple practices that depend on the type of memory environment the program is working in. In general, avoiding memory leaks requires two essential components:

  • An awareness of the environment that the program will be running in
  • Some comprehension of how language that the program is developed in allocates and frees memory

Due to the dependency on the memory environment and language semantics, the majority of information on memory leaks is environment-specific. Good practices may require specific research on the operating system and the language semantics, or general research on the type of memory environment and development model. Refer to the documentation for the operating system and the programming language for more detailed information.

20175 questions
61 answers

How can I create a memory leak in Java?

I just had an interview where I was asked to create a memory leak with Java. Needless to say, I felt pretty dumb, having no idea how to start creating one. What would an example be?
  • 26,356
  • 39
  • 116
  • 178
19 answers

performSelector may cause a leak because its selector is unknown

I'm getting the following warning by the ARC compiler: "performSelector may cause a leak because its selector is unknown". Here's what I'm doing: [_controller performSelector:NSSelectorFromString(@"someMethod")]; Why do I get this warning? I…
Eduardo Scoz
  • 24,653
  • 6
  • 47
  • 62
48 answers

Activity has leaked window that was originally added

What is this error, and why does it happen? 05-17 18:24:57.069: ERROR/WindowManager(18850): Activity com.mypkg.myP has leaked window$DecorView@44c46ff0 that was originally added here 05-17 18:24:57.069:…
  • 204,586
  • 122
  • 423
  • 502
34 answers

Dealing with "java.lang.OutOfMemoryError: PermGen space" error

Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's a typical Hibernate/JPA + IceFaces/JSF application running on Tomcat 6 and JDK 1.6. Apparently this can occur after redeploying an application a…
  • 632
  • 4
  • 11
  • 18
4 answers

What is private bytes, virtual bytes, working set?

I am trying to use the perfmon windows utility to debug memory leaks in a process. This is how perfmon explains the terms: Working Set is the current size, in bytes, of the Working Set of this process. The Working Set is the set of memory pages…
  • 7,642
  • 12
  • 39
  • 60
15 answers

What is a StackOverflowError?

What is a StackOverflowError, what causes it, and how should I deal with them?
  • 21,845
  • 28
  • 75
  • 104
6 answers

If a DOM Element is removed, are its listeners also removed from memory?

If a DOM Element is removed, are its listeners removed from memory too?
Julian Krispel-Samsel
  • 7,512
  • 3
  • 33
  • 40
22 answers

possible EventEmitter memory leak detected

I am getting following warning: (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace: at EventEmitter. (events.js:139:15) at…
  • 9,703
  • 8
  • 38
  • 54
11 answers

Is it necessary to unsubscribe from observables created by Http methods?

Do you need to unsubscribe from Angular 2 http calls to prevent memory leak? fetchFilm(index) { var sub = this._http.get(``) .map(result => result.json()) .map(json => { …
  • 24,129
  • 22
  • 65
  • 104
3 answers

When exactly is it leak safe to use (anonymous) inner classes?

I have been reading some articles on memory leaks in Android and watched this interesting video from Google I/O on the subject. Still, I don't fully understand the concept, and especially when it is safe or dangerous to user inner classes inside an…
  • 13,831
  • 10
  • 55
  • 70
8 answers

This Handler class should be static or leaks might occur: IncomingHandler

I'm developing an Android 2.3.3 application with a service. I have this inside that service to communicate with Main activity: public class UDPListenerService extends Service { private static final String TAG = "UDPListenerService"; …
  • 45,055
  • 107
  • 359
  • 626
5 answers

What kind of leaks does automatic reference counting in Objective-C not prevent or minimize?

In the Mac and iOS platforms, memory leaks are often caused by unreleased pointers. Traditionally, it has always been of utmost importance to check your allocs, copies and retains to make sure each has a corresponding release message. The toolchain…
  • 700,868
  • 160
  • 1,392
  • 1,356
50 answers

Are memory leaks ever OK?

Is it ever acceptable to have a memory leak in your C or C++ application? What if you allocate some memory and use it until the very last line of code in your application (for example, a global object's destructor)? As long as the memory consumption…
  • 3,897
  • 6
  • 40
  • 42
9 answers

Python memory leaks

I have a long-running script which, if let to run long enough, will consume all the memory on my system. Without going into details about the script, I have two questions: Are there any "Best Practices" to follow, which will help prevent leaks from…
  • 33,919
  • 27
  • 84
  • 97
19 answers

How to find memory leak in a C++ code/project?

I am a C++ programmer on the Windows platform. I am using Visual Studio 2008. I usually end up in the code with memory leaks. Normally I find the memory leak by inspecting the code, but it is cumbersome and is not always a good approach. Since I…
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
2 3
99 100