I was reading the C# entry on Wikipedia, and came across:
Managed memory cannot be explicitly freed; instead, it is automatically garbage collected.
Why is it that in languages with automatic memory management, manual management isn't even allowed? I can see that in most cases it wouldn't be necessary, but wouldn't it come in handy where you are tight on memory and don't want to rely on the GC being smart?
This is one of many reasons why languages like Java and C# are slower than C and C++ by design. And it is also the reason why C and C++ don't have and never will have a garbage collector, since those languages prioritize execution speed.
It doesn't prevent memory leaks Nothing ever clears this cache so over time it grows. It hoards every object added to it until the program exits or runs out of memory. Garbage collection can't do anything here. Those objects are technically in use and thus cannot be freed.
Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java.
There are two reasons why C / C++ doesn't have garbage collection. It is "culturally inappropriate". The culture of these languages is to leave storage management to the programmer. It would be technically difficult (and expensive) to implement a precise garbage collector for C / C++.
Languages with automatic memory management are designed to provide substantial memory safety guarantees that can't be offered in the presence of any manual memory management.
Among the problems prevented are
free()
sfree()
on a pointer to memory that you do not own, leading to illegal access in other placesfree()
on a pointer that was not the return value of an allocation function, such as taking the address of some object on the stack or in the middle of an array or other allocation.free()
dAdditionally, automatic management can result in better performance when the GC moves live objects to a consolidated area. This improves locality of reference and hence cache performance.
Garbage collection enforces the type safety of a memory allocator by guaranteeing that memory allocations never alias. That is, if a piece of memory is currently being viewed as a type T
, the memory allocator can guarantee (with garbage collection) that while that reference is alive, it will always refer to a T
. More specifically, it means that the memory allocator will never return that memory as a different type.
Now, if a memory allocator allows for manual free()
and uses garbage collection, it must ensure that the memory you free()
'd is not referenced by anyone else; in other words, that the reference you pass in to free()
is the only reference to that memory. Most of the time this is prohibitively expensive to do given an arbitrary call to free()
, so most memory allocators that use garbage collection do not allow for it.
That isn't to say it is not possible; if you could express a single-referrent type, you could manage it manually. But at that point it would be easier to either stop using a GC language or simply not worry about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With