views:

79

answers:

3

Hello everyone:

We know that malloc() and new operation allocate memory from heap dynamically, but where does heap reside? Does each process have its own private heap in the namespace for dynamic allocation or the OS have a global one shared by all the processes. What's more, I read from a textbook that once memory leak occurs, the missing memory cannot be reused until next time we restart our computer. Is this thesis right? If the answer is yes, how can we explain it?

Thanks for your reply.

Regards.

A: 

You do not mention OS of your interest. That's exactly mean no direct answer.

Try to look into some book about OSes e.g. Tanenbaum's

@gineer I think you mean the implementation is platform dependent. What about the implementation of linux -- I'm not so much interested in Windows :-). And which part of OS textbook I should refer to? Memory Management? Thanks.
Summer_More_More_Tea
+6  A: 

The memory is allocated from the user address space of your process virtual memory. And all the memory is reclaimed by the OS when the process terminates there is no need to restart the computer.

Naveen
+1.Note that memory is not usable by anyone else if process is running. If leak is growing, you will be out of memory soon.
Jack
+1  A: 

Typically the C runtime will use the various OS APIs to allocate memory which is part of its process address space. The, within that allocated memory, it will create a heap and allocate memory itself from that heap via calls to malloc or new.

The reason for this is that often OS APIs are course-grained and require you to allocate memory in large chunks (such as a page size) whereas your application typically wants to allocate small amounts of memory at any one time.

Sean