程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Garbage Collection

Garbage Collection

編輯:關於JAVA

3)Garbage Collection

State the behavior that is guaranteed by the garbage collection system and write code that explicitly makes objects eligible for collection.

1. Garbage collection is a mechanism for reclaiming memory from objects that are no longer in use, and making the memory available for new objects.

2. An object being no longer in use means that it can’t be referenced by any ‘active’ part of the program.

3. Garbage collection runs in a low priority thread. It may kick in when memory is too low. No guarantee.

4. It’s not possible to force garbage collection. Invoking System.gc may start garbage collection process.

5. There are no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all. gc might not even be run if the program execution does not warrant it. Thus any memory allocated during program execution might remain allocated after program termination, unless reclaimed by the OS or by other means.

6. There are also no guarantees on the order in which the objects will be garbage collected or on the order in which the finalizers are called.

7. Circular references do not prevent objects from being garbage collected.

8. We can set the reference variables to null, hinting the gc to garbage collect the objects referred by the variables. Even if we do that, the object may not be gc-ed if it’s attached to a listener. (Typical in case of AWT components) Remember to remove the listener first.

9. All objects have a finalize method. It is inherited from the Object class.

10. finalize method is used to release system resources other than memory. (such as file handles and network connections) The order in which finalize methods are called may not reflect the order in which objects are created. Don’t rely on it. This is the signature of the finalize method.

protected void finalize() throws Throwable { }

In the descendents this method can be protected or public. Descendents can restrict the exception list that can be thrown by this method.

11. finalize is called only once for an object. If any exception is thrown in finalize, the object is still eligible for garbage collection (at the discretion of gc)

12. gc keeps track of unreachable objects and garbage-collects them, but an unreachable object can become reachable again by letting know other objects of its existence from its finalize method (when called by gc). This ‘resurrection’ can be done only once, since finalize is called only one for an object.

13. finalize can be called explicitly, but it does not garbage collect the object.

14. finalize can be overloaded, but only the method with original finalize signature will be called by gc.

15. finalize is not implicitly chained. A finalize method in sub-class should call finalize in super class explicitly as its last action for proper functioning. But compiler doesn’t enforce this check.

16. System.runFinalization can be used to run the finalizers (which have not been executed before) for the objects eligible for garbage collection.

17. Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.

18. Java uses a "mark sweep garbage collection algorithm, which traverses all the object references, marking any objects that are referred to and then garbage collecting any objects that are unmarked.

19. Java allows you to add a finalize() method to any class. The finalize() method will be called before the garbage collector sweeps away the object. In practice, do not rely on the finalize method for recycling any resources that are in short supply - you simply cannot know when this method will be called.

In the exam point of vIEw :

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved