3 ms·
> [1]: Garbage collection is simulating a computer with an infinite amount of memory. But that definition also applies to virtual memory. On any modern OS, you
by pcwalton 8y ago
> [1]: Garbage collection is simulating a computer with an infinite amount of memory.
But that definition also applies to virtual memory. On any modern OS, you can simulate a computer with an infinite amount of memory in C and C++ by just never calling free(). It'll even work pretty well for programs with small working sets, as the kernel will start paging out unused memory to swap.
I think any definition of garbage collection needs to specify that it is a form of memory management: it can automatically determine what memory will be used later so that it can recycle memory that will not.
- zamalek 8y ago> just never calling free() So your code would look a lot like it was running under a garbage collector? The OS memory manager is the garbage collector in this case.
- ecnahc515 8y agoNo, because the OS isn't reclaiming the memory specifically; and this only works with overcommit and/or swap. The memory isn't actually being reclaimed, it's being paged out. If your application actually begins to use the memory but never uses the data again (eg; can't reference it) it will continue to eat memory until the OOM killer kicks in or the system crashes.
- eximius 8y agoWell, unless your program is particularly long lived, it will be reclaimed. There are many tools that could reasonably forego `free` for their entire runtimes.
- ecnahc515 8y agoI presume we're talking about long running programs otherwise the conversation isn't really super useful since for any program, short or long memory is reclaimed when a program exits (well, at least on any modern OS).
- bausshf 8y agoThis is what the D compiler does to keep its speed lmao. Major memory leaks, but who cares it compiles hella fast.