javacommons/utils/tsrc/rtsupport/javasrc/com/nokia/mj/impl/rt/test/FinalizerStatistics.java
changeset 80 d6dafc5d983f
parent 78 71ad690e91f5
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 package com.nokia.mj.impl.rt.test;
       
    18 
       
    19 import java.util.Hashtable;
       
    20 import java.util.Enumeration;
       
    21 
       
    22 public class FinalizerStatistics
       
    23 {
       
    24     private static Hashtable references;
       
    25 
       
    26     static
       
    27     {
       
    28         references = new Hashtable();
       
    29     }
       
    30 
       
    31 
       
    32     /** Class to keep count of finalizable objects */
       
    33     static private class Refcount
       
    34     {
       
    35         /** Current number of alive objects of the class */
       
    36         int count;
       
    37 
       
    38         /** Name of the class */
       
    39         String clazz;
       
    40         private Refcount(String cls)
       
    41         {
       
    42             count = 1;
       
    43             clazz = cls;
       
    44         }
       
    45     }
       
    46 
       
    47     public static void debugFinalizeCreated(String clazz)
       
    48     {
       
    49         System.out.println("Finalizable created: " + clazz);
       
    50         // Add reference to hash
       
    51         synchronized (references)
       
    52         {
       
    53             Refcount r = (Refcount)references.get(clazz);
       
    54             if (r != null)
       
    55             {
       
    56                 r.count++;
       
    57             }
       
    58             else
       
    59             {
       
    60                 references.put(clazz, new Refcount(clazz));
       
    61             }
       
    62         }
       
    63     }
       
    64 
       
    65     public static void debugFinalize(String clazz)
       
    66     {
       
    67         System.out.println("Finalizable destroyed " + clazz);
       
    68         // Reduce reference
       
    69         synchronized (references)
       
    70         {
       
    71             Refcount r = (Refcount)references.get(clazz);
       
    72             r.count--;
       
    73         }
       
    74     }
       
    75 
       
    76     public static void printStatus()
       
    77     {
       
    78         synchronized (references)
       
    79         {
       
    80             StringBuffer sb = new StringBuffer();
       
    81             sb.append("Finalizable objects\n");
       
    82             Enumeration e = references.elements();
       
    83             boolean empty = true;
       
    84             while (e.hasMoreElements())
       
    85             {
       
    86                 Refcount r = (Refcount)e.nextElement();
       
    87 
       
    88                 if (r != null)
       
    89                 {
       
    90                     if (r.count != 0)
       
    91                     {
       
    92                         empty = false;
       
    93                     }
       
    94                     sb.append("  class=" + r.clazz +
       
    95                               " count="  + r.count + "\n");
       
    96                 }
       
    97             }
       
    98             sb.append("\nStatus ");
       
    99             if (empty)
       
   100             {
       
   101                 sb.append("ALL FINALIZED");
       
   102             }
       
   103             else
       
   104             {
       
   105                 sb.append("OBJECTS WAITING FOR FINALIZATION");
       
   106             }
       
   107             references = null;
       
   108             references = new Hashtable();
       
   109             System.out.println(sb.toString());
       
   110         }
       
   111     }
       
   112 }