-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathFinalizerNullsFields.qhelp
More file actions
95 lines (80 loc) · 3.5 KB
/
FinalizerNullsFields.qhelp
File metadata and controls
95 lines (80 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
A finalizer does not need to set an object's fields to <code>null</code> to help the garbage collector. At the point in the Java object life-cycle when the <code>finalize</code> method is
called, the object is no longer reachable from the garbage collection roots. Explicitly setting the object's fields to <code>null</code> does not cause the referenced
objects to be collected by the garbage collector any earlier, and may even adversely affect performance.
</p>
<p>
The life-cycle of a Java object has 7 stages:
</p>
<ul>
<li>
<strong>Created</strong> : Memory is allocated for the object and the initializers and constructors have been run.
</li>
<li>
<strong>In use</strong> : The object is reachable through a chain of strong references from a garbage collection root. A garbage collection
root is a special class of variable (which includes variables on the stack of any thread, static variables of any class, and
references from Java Native Interface code).
</li>
<li>
<strong>Invisible</strong> : The object has already gone out of scope, but the stack frame of the method that contained the scope is still
in memory. Not all objects transition into this state.
</li>
<li>
<strong>Unreachable</strong> : The object is no longer reachable through a chain of strong references. It becomes a candidate for garbage
collection.
</li>
<li>
<strong>Collected</strong> : The garbage collector has identified that the object can be deallocated. If it has a finalizer, it is marked for
finalization. Otherwise, it is deallocated.
</li>
<li>
<strong>Finalized</strong> : An object with a <code>finalize</code> method transitions to this state after the finalize method is completed
and the object still remains unreachable.
</li>
<li>
<strong>Deallocated</strong> : The object is a candidate for deallocation.
</li>
</ul>
<p>
The call to the <code>finalize</code> method occurs when the object is in the 'Collected' stage. At that point, it is already unreachable from the
garbage collection roots so any of its references to other objects no longer contribute to their reference counts.
</p>
</overview>
<recommendation>
<p>
Ensure that the finalizer does not contain any <code>null</code> assignments because they are unlikely to help garbage collection.
</p>
<p>
If a finalizer does nothing but nullify an object's fields, it is best to completely remove the finalizer. Objects with finalizers
severely affect performance, and you should avoid defining <code>finalize</code> where possible.
</p>
</recommendation>
<example>
<p>In the following example, <code>finalize</code> unnecessarily assigns the object's fields to null.</p>
<sample src="FinalizerNullsFields.java" />
</example>
<references>
<li>
J. Bloch, <em>Effective Java (second edition)</em>, Item 7. Addison-Wesley, 2008.
</li>
<li>
IBM developerWorks:
<a href="http://www.ibm.com/developerworks/java/library/j-jtp01274/index.html#3.2">Explicit nulling</a>.
</li>
<li>
Oracle Technology Network:
<a href="http://www.oracle.com/technetwork/articles/javase/finalization-137655.html">
How to Handle Java Finalization's Memory-Retention Issues
</a>.
</li>
<li>
S. Wilson and J. Kesselman, <em>Java Platform Performance: Strategies and Tactics, 1st ed.</em>,
Appendix A. Prentice Hall, 2001.
</li>
</references>
</qhelp>