User 10482
User 10482

Reputation: 1012

Do memory leaks persist after program completion if the OS does not clear it?

My question is how leaked memory is marked as "being used". From my understanding, each program gets a contiguous chunk of memory for all its memory need (stack, heap, read-only, program memory). I imagine once a program completes, all of this would be freed (freed = open for new program to consume). Now, even if I have a memory leak in the program, won't it just automatically free up, when the program memory chunk is freed?

I have been through questions asked about this which say the OS has to free it up, but in some older OS version this does not happen. I don't see why this would be a problem in view of the explanation provided in first paragraph. Also, none talk about what "freeing" actually entails/does.

Upvotes: 5

Views: 874

Answers (1)

David Schwartz
David Schwartz

Reputation: 182769

With modern operating systems, processes don't allocate physical memory. They allocate address space that is backed by physical memory. When the process terminates, its address space no longer exists.

Operating systems long ago actually did allow processes to allocate physical memory. On those, failure to free physical memory would result in the physical memory being permanently lost.

My question is how leaked memory is marked as "being used". From my understanding, each program gets a contiguous chunk of memory for all its memory need (stack, heap, read-only, program memory).

Yes, it gets a contiguous chunk of virtual memory. This virtual memory is backed by physical memory as needed.

I imagine once a program completes, all of this would be freed (freed = open for new program to consume).

Yes, but not. Yes, it's all freed. No, it's not open for a new program to consume because one program cannot possibly consume another process' virtual memory because each process has its own virtual memory space.

Now, even if I have a memory leak in the program, won't it just automatically free up, when the program memory chunk is freed?

It doesn't matter. Virtual memory isn't a scarce resource. It doesn't matter whether it's freed or not. What does matter, however, is that any physical memory that was backing that virtual memory will no longer back it since the virtual memory no longer exists. That frees that physical memory to be used for another purpose.

I have been through questions asked about this which say the OS has to free it up, but in some older OS version this does not happen. I don't see why this would be a problem in view of the explanation provided in first paragraph.

Some older operating systems didn't even have virtual memory support. Some had memory allocation functions that directly allocated physical memory.

Also, none talk about what "freeing" actually entails/does.

On a modern operating system, when it terminates, its virtual address space no longer exists. This may result in some physical pages of memory (actual RAM) having their reference drop to zero which will cause the OS to re-assign them to new uses if necessary.

Upvotes: 13

Related Questions