Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why knowing whether some piece of memory is needed is undecidable?

I was reading the Javascript tutorial of Mozilla and I come through this piece of information.

High-level languages embed a piece of software called "garbage collector" whose job is to track memory allocation and use in order to find when a piece of allocated memory is not needed any longer in which case, it will automatically free it. This process is an approximation since the general problem of knowing whether some piece of memory is needed is undecidable (can't be solved by an algorithm).

I am familiar with the notion of undecidability and garbage collector, but I can't seem to understand why this is an undecidable problem?

like image 976
ralzaul Avatar asked Dec 24 '15 13:12

ralzaul


People also ask

Can a garbage collected language have memory leaks?

Although garbage collection prevents many types of memory leaks, it doesn't prevent all of them. In automatic reference counting systems, such as Perl or Objective-C, memory is leaked whenever there are cyclical references, since the reference count is never decremented to zero.

How could you make sure a const value is garbage collected?

With a const variable declaration, you can't assign to the variable something little like "" or null to clear its contents. That's really the only difference in regard to memory management. Automatic garbage collection is not affected at all by whether it is declared const or not.

What is the purpose of garbage collection in typescript?

The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

What part of memory stack or heap is cleaned in garbage collection process in JavaScript?

Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create an object, it's always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects that don't have any reference.


1 Answers

All garbage collectors I'm familiar with work by collecting memory which can no longer be accessed, e.g. all (the transitive closure of) variables pointing to it went out of scope. But that's an under-approximation of the set of of memory spaces that can be collected, because at any point a memory location may still have a variable pointing to it in scope, yet it will never be accessed again.

Finding the precise set of memory spaces that can be collected is trivially reducible to any undecided problem - for example, find the set of memory spaces that can be collected at point A in the following program:

x = allocate()
// Point A
if (result of some known-to-be-undecidable problem is true):
  print(x)

And so finding that set is undecidable in itself.

like image 53
Oak Avatar answered Oct 12 '22 23:10

Oak