Operating system handling of memory leaks?

In summary, operating systems do not kill processes immediately when they detect a memory leak. They will only shut down programs that try to access memory they don't have access to or take up too much memory. There are also operating environments that can handle memory leaks without crashing.
  • #1
rohanprabhu
414
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?
 
Technology news on Phys.org
  • #2
WinXP doesn't do that. The program may have crashed for unrelated reasons, though.
 
  • #3
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
 
  • #4
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.)
 

1. What is a memory leak?

A memory leak is a type of software bug where a program does not properly manage the memory resources it uses, causing it to allocate memory but not release it when it is no longer needed. This can lead to a gradual depletion of available memory and can ultimately cause the program or entire system to crash.

2. How does an operating system handle memory leaks?

Operating systems have built-in mechanisms to manage memory and detect memory leaks. When a program is executed, the operating system allocates a certain amount of memory to it. As the program runs and requests more memory, the operating system keeps track of the allocated memory and will reclaim it when it is no longer needed. If a memory leak is detected, the operating system will free up the leaked memory and make it available for other programs to use.

3. What causes memory leaks in an operating system?

Memory leaks can be caused by a variety of factors, including programming errors, inefficient memory management, and resource conflicts. They can also occur when a program is terminated before it has a chance to release its allocated memory.

4. How can memory leaks be prevented?

To prevent memory leaks, programmers should carefully manage memory allocation and deallocation in their code. This includes properly freeing up memory when it is no longer needed and avoiding unnecessary memory allocations. Code reviews and testing can also help identify and fix potential memory leaks before they become a problem.

5. Can memory leaks affect the performance of an operating system?

Yes, memory leaks can have a significant impact on the performance of an operating system. As more memory is consumed by leaked resources, less memory is available for other programs to use. This can slow down the system and may eventually cause it to crash. It is important for operating systems to have efficient memory management and detection mechanisms in place to mitigate the effects of memory leaks.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
15
Views
6K
  • Programming and Computer Science
Replies
7
Views
3K
Back
Top