Operating system handling of memory leaks?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 5K views
rohanprabhu
Messages
410
Reaction score
2
do some operating systems kill a thread as soon as it detects a memory leak? The operating system I'm talking about is Windows XP, in this case.

I had a small subroutine, that used to initialize a new variable [dynamic memory allocation], write some value to it and return the pointer [let's call it 'ptrInside']. When the function was called, another pointer was collecting it [let's call it 'ptrOutside'].

What happened is, when I call the subroutine once, it's fine. Because although the pointer declared in the function is killed, there is some variable i.e. 'ptrOutside' which was referring to the allocated space [let's call it 'gothSpace']. But when i called it the second time, 'ptrOutside' was now pointing to some other allocated space [let's call it 'blondeSpace']. So, now that the 'blondeSpace' was getting all the attention, and 'gothSpace' was lonely inside the memory, with no pointer referring to it.

What happened is that, Windows killed the process abruptly, with no reason whatsoever, being the conformist it is.

So, my question is that.. do some operating systems kill processes immediately when they detect that there is a goth in the heap about which nobody in the stack cares about?
 
Physics news on Phys.org
If operating systems were capable of detecting memory leaks, then memory leaks would not exist. No operating system keeps any kind of record of the number of pointers you make to any specific block of memory -- there's no system call involved.

Your program likely crashed because it tried to use a dangling pointer -- a pointer that refers to a block of memory your program no longer owns -- not because of a memory leak.

- Warren
 
rohanprabhu said:
So, my question is that.. do some operating systems kill processes immediately when they detect that there is a goth in the heap about which nobody in the stack cares about?

No. OSs will shut down programs that try to read or write to memory that they don't have access to. (This is called a segmentation fault in the Unix world.) Linux will also shut down programs that take up too much memory -- this may be an issue if you have a bad memory leak.

There are operating environments that will deal with memory leaks - programs loosing track of memory they've allocated, but they don't do reference counting, don't allow C-style pointers, and they won't crash in the way you describe. (You can look up garbage collection on google if you like.)