Home > Software design >  How application memory is freed in operating systems?
How application memory is freed in operating systems?

Time:01-12

I was going through the common issues in memory management. I'm curious to know how is the memory managed in the following case with dynamic memory management in c:

  • I request the memory manager to provide me a free portion through malloc() call.
  • I performed some computations and stored a portion of the data on that section of memory.
  • The memory allocated in not freed.

How does the memory grow on the application. Does it keeps growing (of the some GUI element whose data container is not cleared once allocated).Does it grows each time i open the application untill program terminates (though it is allocated in the normals fashion using some DMA functions).

Will the segment of memory be freed by application during runtime or os doesnt cares of the memory mamagement in such cases ??

CodePudding user response:

OS keeps track of which physical pages of RAM are referenced by which processes, and which pages are free. The exact data-structure can differ based on the OS, so it doesn't really matter. What matters is that when the OS needs to give your process a physical page of RAM, it can allocate it from the pool of free pages. When the process dies, the pages that aren't used anymore can be reclaimed and marked as 'free' again, to be used in future memory allocations.

This is how it works in a nut shell.

Btw, malloc isn't a kernel API. It's part of the C runtime ('libc'), which acquires its memory from the kernel in coarser granularity through means like mmap.

CodePudding user response:

When you start a program, the OS allocates some amount of memory to it. While the program runs, it might request more (with malloc() and the like). When the program terminates normally or is killed, ALL of its memory is freed back to the OS. "Memory leaks" are an issue only for very large programs that stay open for a long time and continually allocate more memory, like a web browser. If such a program, say, requests more memory every time it displays a page, does not free it, and stays open, then it can indeed grow to the point where it causes problems for the OS.

  •  Tags:  
  • Related