In: Computer Science
Which is the correct answer a, b, or c
What variables are deallocated by garbage collection?
a. Global Variable
b. Class field
c. Unreachable objects
When should you call garbage collection?
a. After each method
b. After declaring a class
c. You don't call garbage collection
Answer 1 ) The correct option is c . That is Unreachable Objects are deallocated by garbage collection . This is because garbage collector freeing dynamically allocated memory that is no longer referenced. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. Garbage means Unreferenced Object which means that the objects that are no longer needed by the program are "garbage" and can be thrown away and collects back into the heap, the heap space it occupies must be recycled so that the space is available for the subsequent new objects.
Answer 2 ) The correct option is c. That is You don't call garbage collection. As calling garbage collector manually is not a good practice as garbage collector in Virtual machine contains a lot of sophisticated logic to determine when and what to cleanup. Tuning it requires knowledge about details on how it works. Calling garbage collector manually will affect the system performance drastically, and must not be carried out and so Garbage collector must automatically deallocate the unreferenced memory for better performance.