13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.ref; 27 28 import java.security.PrivilegedAction; 29 import java.security.AccessController; 30 import sun.misc.JavaLangAccess; 31 import sun.misc.SharedSecrets; 32 import sun.misc.VM; 33 34 final class Finalizer extends FinalReference<Object> { /* Package-private; must be in 35 same package as the Reference 36 class */ 37 38 private static ReferenceQueue<Object> queue = new ReferenceQueue<>(); 39 private static Finalizer unfinalized = null; 40 private static final Object lock = new Object(); 41 42 private Finalizer 43 next = null, 44 prev = null; 45 46 private boolean hasBeenFinalized() { 47 return (next == this); 48 } 49 50 private void add() { 51 synchronized (lock) { 52 if (unfinalized != null) { 68 } 69 if (this.next != null) { 70 this.next.prev = this.prev; 71 } 72 if (this.prev != null) { 73 this.prev.next = this.next; 74 } 75 this.next = this; /* Indicates that this has been finalized */ 76 this.prev = this; 77 } 78 } 79 80 private Finalizer(Object finalizee) { 81 super(finalizee, queue); 82 add(); 83 } 84 85 /* Invoked by VM */ 86 static void register(Object finalizee) { 87 new Finalizer(finalizee); 88 } 89 90 private void runFinalizer(JavaLangAccess jla) { 91 synchronized (this) { 92 if (hasBeenFinalized()) return; 93 remove(); 94 } 95 try { 96 Object finalizee = this.get(); 97 if (finalizee != null && !(finalizee instanceof java.lang.Enum)) { 98 jla.invokeFinalize(finalizee); 99 100 /* Clear stack slot containing this variable, to decrease 101 the chances of false retention with a conservative GC */ 102 finalizee = null; 103 } 104 } catch (Throwable x) { } 105 super.clear(); 106 } 107 | 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.ref; 27 28 import java.security.PrivilegedAction; 29 import java.security.AccessController; 30 import sun.misc.JavaLangAccess; 31 import sun.misc.SharedSecrets; 32 import sun.misc.VM; 33 import java.util.TreeMap; 34 import java.util.Set; 35 import java.util.Map; 36 37 final class Finalizer extends FinalReference<Object> { /* Package-private; must be in 38 same package as the Reference 39 class */ 40 41 private static ReferenceQueue<Object> queue = new ReferenceQueue<>(); 42 private static Finalizer unfinalized = null; 43 private static final Object lock = new Object(); 44 45 private Finalizer 46 next = null, 47 prev = null; 48 49 private boolean hasBeenFinalized() { 50 return (next == this); 51 } 52 53 private void add() { 54 synchronized (lock) { 55 if (unfinalized != null) { 71 } 72 if (this.next != null) { 73 this.next.prev = this.prev; 74 } 75 if (this.prev != null) { 76 this.prev.next = this.next; 77 } 78 this.next = this; /* Indicates that this has been finalized */ 79 this.prev = this; 80 } 81 } 82 83 private Finalizer(Object finalizee) { 84 super(finalizee, queue); 85 add(); 86 } 87 88 /* Invoked by VM */ 89 static void register(Object finalizee) { 90 new Finalizer(finalizee); 91 } 92 93 static String printFinalizationQueue() { 94 Finalizer tmp = unfinalized; 95 Map <String, Integer> countMap = new TreeMap<String, Integer>(); 96 synchronized (lock) { 97 while(tmp != null) { 98 Object referent = tmp.get(); 99 if (referent != null) { 100 Class<?> objClass = referent.getClass(); 101 String className = objClass.getName(); 102 Integer cnt = countMap.get(className); 103 if (cnt == null) { 104 countMap.put(className,1); 105 } 106 else { 107 countMap.put(className,cnt + 1); 108 } 109 } 110 tmp = tmp.next; 111 } 112 } 113 114 StringBuilder sb = new StringBuilder(); 115 Set<String> keys = countMap.keySet(); 116 for (String key : keys) { 117 sb.append("Class: " + key + " count: " + countMap.get(key) + "\n"); 118 } 119 120 return sb.toString(); 121 } 122 123 private void runFinalizer(JavaLangAccess jla) { 124 synchronized (this) { 125 if (hasBeenFinalized()) return; 126 remove(); 127 } 128 try { 129 Object finalizee = this.get(); 130 if (finalizee != null && !(finalizee instanceof java.lang.Enum)) { 131 jla.invokeFinalize(finalizee); 132 133 /* Clear stack slot containing this variable, to decrease 134 the chances of false retention with a conservative GC */ 135 finalizee = null; 136 } 137 } catch (Throwable x) { } 138 super.clear(); 139 } 140 |