Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is free() not allowed in garbage-collected languages?

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?

like image 465
Sundar R Avatar asked May 04 '10 14:05

Sundar R


People also ask

What languages are not garbage collected?

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.

Why you must not let garbage collect?

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.

Which programming languages have garbage collection?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java.

Why there is no garbage collection in C?

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++.


2 Answers

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

  • Double free()s
  • Calling free() on a pointer to memory that you do not own, leading to illegal access in other places
  • Calling free() 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.
  • Dereferencing a pointer to memory that has already been free()d

Additionally, 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.

like image 179
Phil Miller Avatar answered Oct 10 '22 07:10

Phil Miller


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.

like image 33
MSN Avatar answered Oct 10 '22 06:10

MSN