-
garbage collection
The realloc question reminded me to ask about this. If memory is allocated/reallocated/freed many times (thousands) over, does the program's memory usage keep growing due to leftover garbage, or does the C runtime or operating system take care of it?
Take the case of a program which compresses lines of text in memory, and decompresses/recompresses lines whenever changes are made (the benefit being memory savings, at the cost of speed.) Assuming garbage collection is automatic, how does it affect the performance of such programs?
-Joe
-
When you call free(), that memory can be re-used in a future malloc / realloc / calloc call.
-
If I free 2 bytes, then never allocate another 2-byte chunk in my program, does the OS wait until the program ends to get the 2 bytes back? Or can another program allocate that 2-bytes?
-Joe
-
The program (in the form of the C run-time library) will typically keep that memory until the end of the program.
If the program doesn't touch that memory for some period of time, the OS will likely swap that memory out to disk so it can be re-used by other programs.
-
In linux, it postpones allocating memory until the last possible second. So if you allocate some memory and only use some of it, the linux kernel will not allocate it until you go to use it. From what I am told though, there is no "runtime" garbage collector like some other languages.