1

While reading a course on the garbage collector I understood that it removes unreferenced objects from memory, I tried to answer some questions in a quiz related to the subject and I found this question:

The role of the garbage collector is to ensure that there is enough memory to run a Java program?

Is it true or false? I know it manages memory, but does it guarantee that there is enough memory to run a program?

user395817
  • 185
  • 2
  • 2
  • 12
  • 3
    false. it removes unreferenced objects from memory, you are right. – The Scientific Method Aug 27 '18 at 18:43
  • 2
    The amount of available memory doesn’t change. It’s the amount of *free* memory which the garbage collector tries to raise. Whether “to ensure” implies “guarantees” or allows to be interpreted as “tries at best effort”, I don’t know. Reads bad for a quiz question, being colloquial at a place where precise words are needed… – Holger Aug 28 '18 at 08:22

1 Answers1

1

It does, what it sounds exactly:

"Garbage collection"

Garbage collector has not a responsibility of memory allocation or ensuring there is enough memory to run program.

So take a look at wikipedia about garbage collector : https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

Also according to oracle :

What is Automatic Garbage Collection? Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.

https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

And check that stacoverflow questiont also : What's the difference between memory allocation and garbage collection, please?

Emre Savcı
  • 3,034
  • 2
  • 16
  • 25