86
votes
Accepted
Why don't compilers automatically insert deallocations?
Because it's undecidable whether the program will use the memory again. This means that no algorithm can correctly determine when to call free() in all cases, which ...
58
votes
Why don't compilers automatically insert deallocations?
As David Richerby rightly noted, the problem is undecidable in general. Object liveness is a global property of the program, and may in general depend on the inputs to the program.
Even precise ...
30
votes
Why don't compilers automatically insert deallocations?
It's an incompleteness problem, not an undecidability problem
While it's true that the optimal placement of deallocation statements is undecidable, that's simply not the issue here. Since it's ...
24
votes
Why don't compilers automatically insert deallocations?
Currently, none of the posted answers are fully correct. It's not impossible to do this, but adding that feature would restrict mermory allocation patterns.
Why don't compilers automatically insert ...
10
votes
Accepted
Is reference counting GC vs. tracing GC a language property or an implementation property?
Swift guarantees that once the last reference to an object is dropped the object is deinitialized, and the deinit code is immediately run.
Obtaining this kind of ...
10
votes
Why don't compilers automatically insert deallocations?
"Humans do it, so it's not impossible" is a well-known fallacy. We do not necessarily understand (let alone control) the things that we create - money is a common example. We tend to overestimate (...
9
votes
Why don't compilers automatically insert deallocations?
The lack of automatic memory management is a feature of the language.
C is not supposed to be a tool for writing software easily. It is a tool for making the computer do whatever you tell it to do. ...
9
votes
Why don't compilers automatically insert deallocations?
The issue is mostly a historic artifact, not an impossibility of implementation.
The way most C compilers build code is so that the compiler only sees each source file at a time; it never sees the ...
9
votes
Accepted
Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?
No, it's not possible for a compiler to do that. Doing so is an undecidable problem.
To get some intuition for why it is hard, consider the combinations of loops and conditional statements. It's not ...

D.W.♦
- 156k
6
votes
Why don't compilers automatically insert deallocations?
Other answers have focussed on whether it is possible to do garbage collection, some details of how it's done, and some of the problems.
One issue which hasn't yet been covered though is the ...
5
votes
Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?
What D.W. said is correct in one way: It is impossible for a compiler in general to decide that at point X in a program, object O is not referenced anymore and can be deallocated.
However, you can (...
4
votes
Is reference counting GC vs. tracing GC a language property or an implementation property?
chi has answered the specific question in the body about swift, this answer answers the more general question in the title.
Is reference counting GC vs. tracing GC a language property or an ...
4
votes
Accepted
Why does garbage collection extend only to memory and not other resource types?
GC deals with a predictable and reserved resource. The VM has total control over it and has total control over what instances are created and when. The keywords here are "reserved" and "total control"....
4
votes
Why don't compilers automatically insert deallocations?
What is done: There is garbage collection, and there are compilers using reference counting (Objective-C, Swift). Those that do reference counting need help from the programmer by avoiding strong ...
4
votes
Why don't compilers automatically insert deallocations?
The question presumes a deallocation is something the programmer is supposed to deduce from other parts of the source code. It's not. "At this point in the program, memory reference FOO isn't useful ...
4
votes
Accepted
Is it possible to implement a WeakMap with primitive keys and weak values?
Okay. My confusion with this question is that I assumed that what you wanted is a weak value map, which it appears is what you want, and this can be implemented in a straightforward manner (at least ...
3
votes
Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?
... if a compiler would simulate every conditional statement in a program ...
You forget about programs that have some input:
Let's say we have two variables: A ...
3
votes
Why don't compilers automatically insert deallocations?
In languages like C, the programmer is expected to insert calls to free. Why doesn't the compiler do this automatically?
Because the lifetime of a memory block is the programmer's decision, not the ...
3
votes
Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?
For a more practical example, imagine writing a program that reads in a graph, performs some operations on it, and prints it out. It's possible a command in the middle deletes a node, dropping half ...
3
votes
Why does garbage collection extend only to memory and not other resource types?
There are many programming techniques to help manage these kinds of resources.
C++ programmers often use a pattern called Resource Acquisition is Initialization, or RAII for short. This pattern ...

D.W.♦
- 156k
3
votes
Why does garbage collection extend only to memory and not other resource types?
This appears to be one of the reasons languages with garbage collectors implements finalizers. Finalizers are intended to allow a programmer to clean up an object's resources during garbage ...
3
votes
What are some possible "functional" memory structures?
The topic is fairly broad.
Functional hardware has been explored in e.g. Lisp machines.
More on the theoretical side, you may want to read about the various abstract machines which have been ...
2
votes
Why does garbage collection extend only to memory and not other resource types?
All memory is equal, if I ask for 1K, I don’t care where in the address space the 1K comes from.
When I ask for a file handle, I want a handle to the file I wish to open. Having a file handle open ...
2
votes
Why is the object destructor paradigm in garbage collected languages pervasively absent?
Why is the object destructor paradigm in garbage collected languages pervasively absent?
I come from a C++ background, so this area is baffling to me.
The destructor in C++ actually does two things ...
2
votes
Why is the object destructor paradigm in garbage collected languages pervasively absent?
When the of two most popular GC frameworks (Java and .NET) were being designed, I think the authors expected that finalization would work well enough to avoid the need for other forms of resource ...
2
votes
delete node from linked list (lang w/GC) -should deleted item 'next' be set to null
If you mean the next pointer of the node that was deleted, as far as I know it is normally not necessary to set the pointer to null. Once the node is deleted, if the code is not buggy, then the code ...

D.W.♦
- 156k
1
vote
Is it possible to implement a WeakMap with primitive keys and weak values?
It would be pointless to have a primitive as the key in a Javascript WeakMap, as you can't/don't garbage-collect primitives, and a WeakMap is entirely about whether or not GC will garbage-collect the ...

D.W.♦
- 156k
1
vote
Accepted
What would a language look like in which precise GC was implementable as a library?
I believe that this is possible, or at least nearly possible, in a language like Rust, though perhaps not necessarily in the sense you are thinking.
Rust actually has a GC library, though I can't say ...
1
vote
What would a language look like in which precise GC was implementable as a library?
I think it is possible to implement a garbage collector in C++ without changing the language itself. But in order to use the garbage collector, one must restrict the programmer from using arbitrary ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
garbage-collection × 22memory-management × 13
programming-languages × 8
compilers × 4
memory-allocation × 2
language-design × 2
algorithms × 1
graphs × 1
data-structures × 1
terminology × 1
computer-architecture × 1
reference-request × 1
lambda-calculus × 1
functional-programming × 1
concurrency × 1
efficiency × 1
cpu-cache × 1
cpu × 1
memory-hardware × 1
semantics × 1
linked-lists × 1
paging × 1
resource-allocation × 1
lisp × 1
empirical-research × 1