use of java.lang.ref.Reference in project jdk8u_jdk by JetBrains.
the class DGCImplInsulation method main.
public static void main(String[] args) throws Exception {
TestLibrary.suggestSecurityManager(null);
Permissions perms = new Permissions();
perms.add(new SocketPermission("*:1024-", "listen"));
AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(new CodeSource(null, (Certificate[]) null), perms) });
Remote impl = new DGCImplInsulation();
;
try {
Remote stub = (Remote) java.security.AccessController.doPrivileged(new ExportAction(impl));
System.err.println("exported remote object; local stub: " + stub);
MarshalledObject mobj = new MarshalledObject(stub);
stub = (Remote) mobj.get();
System.err.println("marshalled/unmarshalled stub: " + stub);
ReferenceQueue refQueue = new ReferenceQueue();
Reference weakRef = new WeakReference(impl, refQueue);
impl = null;
System.gc();
if (refQueue.remove(TIMEOUT) == weakRef) {
throw new RuntimeException("TEST FAILED: remote object garbage collected");
} else {
System.err.println("TEST PASSED");
stub = null;
System.gc();
Thread.sleep(2000);
System.gc();
}
} finally {
try {
UnicastRemoteObject.unexportObject(impl, true);
} catch (Exception e) {
}
}
}
use of java.lang.ref.Reference in project ACS by ACS-Community.
the class GarbageCollectionTestHelper method waitForGC.
/**
* a simple algorithm to wait for GC
*/
public boolean waitForGC(long maxTimeout) {
long startTime = System.currentTimeMillis();
long timeOut = maxTimeout;
Reference retRef = null;
System.gc();
while (true) {
try {
retRef = refQ.remove(timeOut);
break;
} catch (InterruptedException ex) {
long delta = System.currentTimeMillis() - startTime;
if (delta < maxTimeout) {
timeOut = maxTimeout - delta;
continue;
}
}
// catch
}
return (retRef != null);
}
use of java.lang.ref.Reference in project ACS by ACS-Community.
the class GarbageCollectionTestHelper method complexWaitForGC.
/**
* a more sophisticated algorithm to wait for Property Change Events
*/
public boolean complexWaitForGC(long maxTimeout) {
long startTime = System.currentTimeMillis();
long timeOut = maxTimeout;
Reference retRef = null;
int slices = (int) (maxTimeout / GC_SLICE_MILLIS);
//@
System.err.println("waiting for " + slices + " slices");
for (int ix = 0; ix < slices; ix++) {
System.gc();
//@
System.err.println("sleeping for " + GC_SLICE_MILLIS);
try {
Thread.currentThread().sleep(GC_SLICE_MILLIS);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
retRef = refQ.poll();
if (retRef != null) {
return true;
}
}
return (retRef != null);
}
use of java.lang.ref.Reference in project ignite by apache.
the class CacheWeakQueryIteratorsHolder method checkWeakQueue.
/**
* Closes unreachable iterators.
*/
public void checkWeakQueue() {
for (Reference itRef = refQueue.poll(); itRef != null; itRef = refQueue.poll()) {
try {
WeakReference weakRef = (WeakReference) itRef;
AutoCloseable rsrc = refs.remove(weakRef);
if (rsrc != null)
rsrc.close();
} catch (Exception e) {
U.error(log, "Failed to close iterator.", e);
}
}
}
use of java.lang.ref.Reference in project intellij-community by JetBrains.
the class DebugReflectionUtil method queueStronglyReferencedValues.
private static void queueStronglyReferencedValues(Queue<BackLink> queue, @NotNull Object root, @NotNull Condition<Object> shouldExamineValue, @NotNull BackLink backLink) {
Class rootClass = root.getClass();
for (Field field : getAllFields(rootClass)) {
String fieldName = field.getName();
// do not follow weak/soft refs
if (root instanceof Reference && "referent".equals(fieldName))
continue;
Object value;
try {
value = field.get(root);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
queue(value, field, backLink, queue, shouldExamineValue);
}
if (rootClass.isArray()) {
try {
//noinspection ConstantConditions
for (Object value : (Object[]) root) {
queue(value, null, backLink, queue, shouldExamineValue);
}
} catch (ClassCastException ignored) {
}
}
// check for objects leaking via static fields. process initialized classes only
if (root instanceof Class && isInitialized((Class) root)) {
for (Field field : getAllFields((Class) root)) {
if ((field.getModifiers() & Modifier.STATIC) == 0)
continue;
try {
Object value = field.get(null);
queue(value, field, backLink, queue, shouldExamineValue);
} catch (IllegalAccessException ignored) {
}
}
}
}
Aggregations