What happens if memory is leaking?

When your process allocates memory from the OS on an ongoing basis, and never frees up any of it, you will eventually be using more memory than there is physically in the machine. At this point, the OS will first swap out to virtual memory (degrades performance) if it has any, and at some point your process will reach a point where the OS can no longer grant it more memory, because you've exceeded the maximum amount of addressable space (4GB on a 32bit OS).

There are basically two reasons this can happen: You've allocated memory and you've lost the pointer to it (it has become unreachable to your program), so you cannot free it any longer. That's what most people call a memory leak. Alternatively, you may just be allocating memory and never freeing it, because your program is lazy. that's not so much a leak, but in the end, the problems you get into are the same ones.


A memory leak is when your code allocates memory and then loses track of it, including the ability to free it later.

In C, for example, this can be done with the simple sequence:

void *pointer = malloc (2718); // Alloc, store address in pointer.
pointer = malloc (31415);      // And again.
free (pointer);                // Only frees the second block.

The original block of memory is still allocated but, because pointer no longer points to it, you have no way to free it.

That sequence, on its own, isn't that bad (well, it is bad, but the effects may not be). It's usually when you do it repeatedly that problems occur. Such as in a loop, or in a function that's repeatedly called:

static char firstDigit (int val) {
    char *buff = malloc (100);      // Allocates.
    if (val < 0)
        val = -val;
    sprintf (buff, "%d", val);
    return buff[0];                 // But never frees.
}

Every time you call that function, you will leak the hundred bytes (plus any housekeeping information).

And, yes, memory leaks will affect other things. But the effects should be limited.

It will eventually affect the process that is leaking as it runs out of address space for allocating more objects. While that may not necessarily matter for short-lived processes, long-lived processes will eventually fail.

However, a decent operating system (and that includes Windows) will limit the resources that a single process can use, which will minimise the effects on other processes. Since modern environments disconnect virtual from physical memory, the only real effect that can be carried from process to process is if one tries to keep all its virtual memory resident in physical memory all the time, reducing the allocation of that physical memory to other processes.

But, even if a single process leaks gigabytes of memory, the memory itself won't be being used by the process (the crux of the leak is that the process has lost access to the memory). And, since it's not being used, the OS will almost certainly swap it out to disk and never have to bring it back into RAM again.

Of course, it uses up swap space and that may affect other processes but the amount of disk far outweighs the amount of physical RAM.