A few questions about Java garbage collection

1. What is the purpose of Java garbage collection? When does garbage collection take place?

A: The purpose of garbage collection is to identify and discard objects that are no longer used in an application to free and reuse resources.

2. What will System.gc() and Runtime.gc() do?

Answer: These two methods are used to prompt the JVM for garbage collection. However, it is up to the JVM whether to recycle immediately or deferred.

3. When is the finalize() method called? What is the purpose of the destructor?

Answer: Before freeing the memory occupied by the object, the garbage collector will call the finalize() method of the object. It is generally recommended to release resources held by the object in this method.

4. If the reference of the object is set to null, will the garbage collector immediately release the memory occupied by the object?

Answer: No, in the next garbage collection cycle, this object will be recyclable.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324617326&siteId=291194637