-2

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 ??

Surya Bhusal
  • 520
  • 2
  • 8
  • 28
  • 5
    OS giveth memory, OS taketh away on process termination. – a3f Jan 11 '22 at 18:32
  • Does OS haves its own garbage collector like things ?? – Surya Bhusal Jan 11 '22 at 18:34
  • You didn't mention `free()`. You `malloc()` memory, and then you `free()` it when you no longer need it. If you do not `free()`, the memory remains allocated until the process ends. – Robert Harvey Jan 11 '22 at 18:34
  • Does it grows every time the program reaches that point of execution ?? – Surya Bhusal Jan 11 '22 at 18:36
  • 1
    [It's complicated](https://www.kernel.org/doc/html/latest/admin-guide/mm/index.html). – Robert Harvey Jan 11 '22 at 18:36
  • But from the C program's perspective, it is exactly as I said. – Robert Harvey Jan 11 '22 at 18:37
  • @RobertHarvey, the question is not about Linux. – 0andriy Jan 11 '22 at 23:15
  • 1
    @0andriy: The question is not about Windows either. Each operating system handles memory requests differently. – Robert Harvey Jan 12 '22 at 13:25
  • @RobertHarvey you are so fixed on the OS implementations rather than common principles. – 0andriy Jan 12 '22 at 13:33
  • @0andriy: Alright. But I also stated (more or less) that all you really need to know is how to use malloc and free properly. – Robert Harvey Jan 12 '22 at 13:35
  • @0andriy this is nearly impossible to answer as its written because it's dependent on the OS, CPU, MMU etc. All of which can change. C runs in many different environments from the mars rovers (which actually have some C++ too) to your computer. But they have very different memory management strategies for each OS. The rovers statically allocate almost everything for each subsystem. Whereas even Windows and Linux do dramatically different approaches. – Mgetz Jan 12 '22 at 14:47
  • @Mgetz, why? Are you suggesting that university course on OS architecture worth nothing? If everyone will do like you suggest, it would be impossible to create anything for wide use (don’t trap yourself in implementation details). The basic principles are the same. – 0andriy Jan 12 '22 at 15:06

2 Answers2

1

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.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
0

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.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55