I have an issue with memory management in various operating systems.
My program is a server that does some processing that could take a few GB of memory. After that, it releases most of the memory while it waits for a few hours until another request arrives.
On AIX and Solaris, I observe the following behavior,
When I free memory, the memory is not returned back to the operating system. The amount of virtual memory used by a process always increases - never decreases. The same is true for the physical memory, up to its limit. Thus it appears that we use all this memory in sleep mode as well.
When this memory can be returned back to OS? How can I make it?
Linux is different: it appears that is does return memory sometimes, but I'm not able to understand when and how. I have for example a scenario in which the process before a request was 100MB, then 700MB at the peak, and after releasing all that it was down to 600MB. I don't understand it - if Linux gives back memory to the OS, why not all of it?
the way memory is allocated (and maybe given back to the OS) is in the libc, i assume. The Programming language / library stack you are using might be of reason for this.
I assume glibc will return non-fragmented memory at the top of the heap. Your process might allocate 10MB of data it will use all the time. After that, 500MB of data that is used in processing will be allocated. After that, a tiny fragment of data that is kept even after the processing (might be the result of the processing) is allocated. After that another 500MB is allocated Memory layout is:
|10MB used|500 MB processing|1MB result|500MB processing| = 1011 MB total
When the 1000MB are freed, the memory layout is
|10MB used|500MB freed|1MB result|500 MB freed| glibc might now return the memory at the end... |10MB used|500MB freed|1MB result| = 511 MB "in use" also only 11MB of that is used.
I assume that is what happens, you'll need to do further research (seperate memory pools come to mind) to ensure all memory will be freed