asked 68.9k views
5 votes
which of the following statements are true about garbage collection in java? pick one or more options the garbage collector can only free memory that was allocated using the new keyword. the garbage collector can free memory that is being referenced by a static variable. the system.gc() method can be used to guarantee that garbage collection will occur. the finalize() method can be used to prevent an object from being garbage collected.

asked
User Darxsys
by
7.8k points

1 Answer

6 votes

The garbage collector can only free memory that was allocated using the new keyword.

The finalize() method can be used to perform cleanup of dynamically allocated resources before garbage collection occurs.

The statement "the garbage collector can free memory that is being referenced by a static variable" is false. The garbage collector only frees memory that is no longer referenced by any live object. A static variable can keep an object alive even if the object is not referenced by any other object.

Therefore, the only true statement about garbage collection in Java is "the garbage collector can only free memory that was allocated using the new keyword."

answered
User Martin Zahariev
by
8.7k points