< prev index next >

src/java.rmi/share/classes/sun/rmi/transport/GC.java

Print this page
rev 15152 : 8157570: sun.rmi.transport.GC retains a strong reference to the context class loader
Reviewed-by: alanb, dfuchs, msheppar


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 sun.rmi.transport;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.util.SortedSet;
  31 import java.util.TreeSet;

  32 
  33 
  34 /**
  35  * Support for garbage-collection latency requests.
  36  *
  37  * @author   Mark Reinhold
  38  * @since    1.2
  39  */
  40 
  41 class GC {
  42 
  43     private GC() { }            /* To prevent instantiation */
  44 
  45 
  46     /* Latency-target value indicating that there's no active target
  47      */
  48     private static final long NO_TARGET = Long.MAX_VALUE;
  49 
  50     /* The current latency target, or NO_TARGET if there is no target
  51      */


  73      * <p> For simple stop-the-world collectors this value is just the time
  74      * since the most recent collection.  For generational collectors it is the
  75      * time since the oldest generation was most recently collected.  Other
  76      * collectors are free to return a pessimistic estimate of the elapsed
  77      * time, or simply the time since the last full collection was performed.
  78      *
  79      * <p> Note that in the presence of reference objects, a given object that
  80      * is no longer strongly reachable may have to be inspected multiple times
  81      * before it can be reclaimed.
  82      */
  83     public static native long maxObjectInspectionAge();
  84 
  85     static {
  86         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  87             public Void run() {
  88                 System.loadLibrary("rmi");
  89                 return null;
  90             }});
  91     }
  92 
  93     private static class Daemon extends Thread {
  94 

  95         public void run() {
  96             for (;;) {
  97                 long l;
  98                 synchronized (lock) {
  99 
 100                     l = latencyTarget;
 101                     if (l == NO_TARGET) {
 102                         /* No latency target, so exit */
 103                         GC.daemon = null;
 104                         return;
 105                     }
 106 
 107                     long d = maxObjectInspectionAge();
 108                     if (d >= l) {
 109                         /* Do a full collection.  There is a remote possibility
 110                          * that a full collection will occurr between the time
 111                          * we sample the inspection age and the time the GC
 112                          * actually starts, but this is sufficiently unlikely
 113                          * that it doesn't seem worth the more expensive JVM
 114                          * interface that would be required.
 115                          */
 116                         System.gc();
 117                         d = 0;
 118                     }
 119 
 120                     /* Wait for the latency period to expire,
 121                      * or for notification that the period has changed
 122                      */
 123                     try {
 124                         lock.wait(l - d);
 125                     } catch (InterruptedException x) {
 126                         continue;
 127                     }
 128                 }
 129             }
 130         }
 131 
 132         private Daemon(ThreadGroup tg) {
 133             super(tg, null, "GC Daemon", 0L, false);
 134         }
 135 
 136         /* Create a new daemon thread in the root thread group */
 137         public static void create() {
 138             PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {
 139                 public Void run() {
 140                     ThreadGroup tg = Thread.currentThread().getThreadGroup();
 141                     for (ThreadGroup tgn = tg;
 142                          tgn != null;
 143                          tg = tgn, tgn = tg.getParent());
 144                     Daemon d = new Daemon(tg);
 145                     d.setDaemon(true);
 146                     d.setPriority(Thread.MIN_PRIORITY + 1);
 147                     d.start();
 148                     GC.daemon = d;
 149                     return null;
 150                 }};
 151             AccessController.doPrivileged(pa);
 152         }
 153 
 154     }
 155 
 156 
 157     /* Sets the latency target to the given value.
 158      * Must be invoked while holding the lock.
 159      */
 160     private static void setLatencyTarget(long ms) {
 161         latencyTarget = ms;
 162         if (daemon == null) {
 163             /* Create a new daemon thread */
 164             Daemon.create();
 165         } else {
 166             /* Notify the existing daemon thread
 167              * that the lateency target has changed
 168              */




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 sun.rmi.transport;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.util.SortedSet;
  31 import java.util.TreeSet;
  32 import jdk.internal.misc.InnocuousThread;
  33 
  34 
  35 /**
  36  * Support for garbage-collection latency requests.
  37  *
  38  * @author   Mark Reinhold
  39  * @since    1.2
  40  */
  41 
  42 class GC {
  43 
  44     private GC() { }            /* To prevent instantiation */
  45 
  46 
  47     /* Latency-target value indicating that there's no active target
  48      */
  49     private static final long NO_TARGET = Long.MAX_VALUE;
  50 
  51     /* The current latency target, or NO_TARGET if there is no target
  52      */


  74      * <p> For simple stop-the-world collectors this value is just the time
  75      * since the most recent collection.  For generational collectors it is the
  76      * time since the oldest generation was most recently collected.  Other
  77      * collectors are free to return a pessimistic estimate of the elapsed
  78      * time, or simply the time since the last full collection was performed.
  79      *
  80      * <p> Note that in the presence of reference objects, a given object that
  81      * is no longer strongly reachable may have to be inspected multiple times
  82      * before it can be reclaimed.
  83      */
  84     public static native long maxObjectInspectionAge();
  85 
  86     static {
  87         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  88             public Void run() {
  89                 System.loadLibrary("rmi");
  90                 return null;
  91             }});
  92     }
  93 
  94     private static class Daemon implements Runnable {
  95 
  96         @Override
  97         public void run() {
  98             for (;;) {
  99                 long l;
 100                 synchronized (lock) {
 101 
 102                     l = latencyTarget;
 103                     if (l == NO_TARGET) {
 104                         /* No latency target, so exit */
 105                         GC.daemon = null;
 106                         return;
 107                     }
 108 
 109                     long d = maxObjectInspectionAge();
 110                     if (d >= l) {
 111                         /* Do a full collection.  There is a remote possibility
 112                          * that a full collection will occurr between the time
 113                          * we sample the inspection age and the time the GC
 114                          * actually starts, but this is sufficiently unlikely
 115                          * that it doesn't seem worth the more expensive JVM
 116                          * interface that would be required.
 117                          */
 118                         System.gc();
 119                         d = 0;
 120                     }
 121 
 122                     /* Wait for the latency period to expire,
 123                      * or for notification that the period has changed
 124                      */
 125                     try {
 126                         lock.wait(l - d);
 127                     } catch (InterruptedException x) {
 128                         continue;
 129                     }
 130                 }
 131             }
 132         }
 133 
 134         /* Create a new daemon thread */




 135         public static void create() {
 136             PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {
 137                 public Void run() {
 138                     Thread t = InnocuousThread.newSystemThread("RMI GC Daemon",
 139                                                                new Daemon());
 140                     assert t.getContextClassLoader() == null;
 141                     t.setDaemon(true);
 142                     t.setPriority(Thread.MIN_PRIORITY + 1);
 143                     t.start();
 144                     GC.daemon = t;


 145                     return null;
 146                 }};
 147             AccessController.doPrivileged(pa);
 148         }
 149 
 150     }
 151 
 152 
 153     /* Sets the latency target to the given value.
 154      * Must be invoked while holding the lock.
 155      */
 156     private static void setLatencyTarget(long ms) {
 157         latencyTarget = ms;
 158         if (daemon == null) {
 159             /* Create a new daemon thread */
 160             Daemon.create();
 161         } else {
 162             /* Notify the existing daemon thread
 163              * that the lateency target has changed
 164              */


< prev index next >