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.

Introduction

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.

18870 questions
3428
votes
59 answers

How can I create a memory leak in Java?

I just had an interview, and I was asked to create a memory leak with Java. Needless to say, I felt pretty dumb having no clue on how to even start creating one. What would an example be?
MatBanik
  • 24,206
  • 38
  • 107
  • 172
1279
votes
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,183
  • 6
  • 45
  • 62
1230
votes
32 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…
Chris
  • 562
  • 4
  • 9
  • 18
1223
votes
44 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 com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here 05-17 18:24:57.069:…
Pentium10
  • 190,605
  • 114
  • 394
  • 474
518
votes
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…
pankajt
  • 7,024
  • 11
  • 33
  • 59
471
votes
16 answers

What is a StackOverflowError?

What is a StackOverflowError, what causes it, and how should I deal with them?
Ziggy
  • 20,298
  • 23
  • 71
  • 99
372
votes
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
  • 6,792
  • 3
  • 31
  • 40
336
votes
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…
Sébastien
  • 11,400
  • 8
  • 46
  • 63
306
votes
7 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"; …
VansFannel
  • 41,682
  • 96
  • 329
  • 561
267
votes
20 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…
Riz
  • 8,611
  • 7
  • 33
  • 52
258
votes
10 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(`http://example.com`) .map(result => result.json()) .map(json => { …
born2net
  • 20,205
  • 21
  • 54
  • 94
237
votes
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…
Imbue
  • 3,637
  • 5
  • 37
  • 41
236
votes
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…
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
210
votes
38 answers

Is there a good Valgrind substitute for Windows?

I was looking into Valgrind to help improve my C coding/debugging when I discovered it is only for Linux - I have no other need or interest in moving my OS to Linux so I was wondering if there is a equally good program for Windows.
Drake
  • 1,938
  • 3
  • 18
  • 25
204
votes
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…
Chris_vr
  • 6,210
  • 15
  • 59
  • 119
1
2 3
99 100