|
Java example source code file (GC.java)
The GC.java Java example source code
/*
* Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.misc;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Support for garbage-collection latency requests.
*
* @author Mark Reinhold
* @since 1.2
*/
public class GC {
private GC() { } /* To prevent instantiation */
/* Latency-target value indicating that there's no active target
*/
private static final long NO_TARGET = Long.MAX_VALUE;
/* The current latency target, or NO_TARGET if there is no target
*/
private static long latencyTarget = NO_TARGET;
/* The daemon thread that implements the latency-target mechanism,
* or null if there is presently no daemon thread
*/
private static Thread daemon = null;
/* The lock object for the latencyTarget and daemon fields. The daemon
* thread, if it exists, waits on this lock for notification that the
* latency target has changed.
*/
private static class LatencyLock extends Object { };
private static Object lock = new LatencyLock();
/**
* Returns the maximum <em>object-inspection age, which is the number
* of real-time milliseconds that have elapsed since the
* least-recently-inspected heap object was last inspected by the garbage
* collector.
*
* <p> For simple stop-the-world collectors this value is just the time
* since the most recent collection. For generational collectors it is the
* time since the oldest generation was most recently collected. Other
* collectors are free to return a pessimistic estimate of the elapsed
* time, or simply the time since the last full collection was performed.
*
* <p> Note that in the presence of reference objects, a given object that
* is no longer strongly reachable may have to be inspected multiple times
* before it can be reclaimed.
*/
public static native long maxObjectInspectionAge();
private static class Daemon extends Thread {
public void run() {
for (;;) {
long l;
synchronized (lock) {
l = latencyTarget;
if (l == NO_TARGET) {
/* No latency target, so exit */
GC.daemon = null;
return;
}
long d = maxObjectInspectionAge();
if (d >= l) {
/* Do a full collection. There is a remote possibility
* that a full collection will occurr between the time
* we sample the inspection age and the time the GC
* actually starts, but this is sufficiently unlikely
* that it doesn't seem worth the more expensive JVM
* interface that would be required.
*/
System.gc();
d = 0;
}
/* Wait for the latency period to expire,
* or for notification that the period has changed
*/
try {
lock.wait(l - d);
} catch (InterruptedException x) {
continue;
}
}
}
}
private Daemon(ThreadGroup tg) {
super(tg, "GC Daemon");
}
/* Create a new daemon thread in the root thread group */
public static void create() {
PrivilegedAction<Void> pa = new PrivilegedAction
Other Java examples (source code examples)Here is a short list of links related to this Java GC.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.