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 jdk8u_jdk by JetBrains.
the class ConstantPoolInfoGC method runTests.
/********** test core **********/
protected void runTests() throws Exception {
targetClass = startToMain("ConstantPoolGCTarg").location().declaringType();
if (vm().canGetConstantPool()) {
byte[] cpbytes = targetClass.constantPool();
// imitate SoftReference cleared
Field constantPoolBytesRef = ReferenceTypeImpl.class.getDeclaredField("constantPoolBytesRef");
constantPoolBytesRef.setAccessible(true);
Reference softRef = (Reference) constantPoolBytesRef.get(targetClass);
softRef.clear();
byte[] cpbytes2 = targetClass.constantPool();
if (!Arrays.equals(cpbytes, cpbytes2)) {
failure("Consequent constantPool results vary, first was : " + cpbytes + ", now: " + cpbytes2);
}
;
} else {
System.out.println("can get constant pool version not supported");
}
/*
* resume until end
*/
listenUntilVMDisconnect();
/*
* deal with results of test
* if anything has called failure("foo") testFailed will be true
*/
if (!testFailed) {
println("ConstantPoolInfoGC: passed");
} else {
throw new Exception("ConstantPoolInfoGC: failed");
}
}
use of java.lang.ref.Reference in project jdk8u_jdk by JetBrains.
the class RuntimeThreadInheritanceLeak method main.
public static void main(String[] args) {
System.err.println("\nRegression test for bug 4404702\n");
/*
* HACK: Work around the fact that java.util.logging.LogManager's
* (singleton) construction also has this bug-- it will register a
* "shutdown hook", i.e. a thread, which will inherit and pin the
* current thread's context class loader for the lifetime of the VM--
* by causing the LogManager to be initialized now, instead of by
* RMI when our special context class loader is set.
*/
java.util.logging.LogManager.getLogManager();
/*
* HACK: Work around the fact that the non-native, thread-based
* SecureRandom seed generator (ThreadedSeedGenerator) seems to
* have this bug too (which had been causing this test to fail
* when run with jtreg on Windows XP-- see 4910382).
*/
(new java.security.SecureRandom()).nextInt();
RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak();
try {
ClassLoader loader = URLClassLoader.newInstance(new URL[0]);
ReferenceQueue refQueue = new ReferenceQueue();
Reference loaderRef = new WeakReference(loader, refQueue);
System.err.println("created loader: " + loader);
Thread.currentThread().setContextClassLoader(loader);
UnicastRemoteObject.exportObject(obj);
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
System.err.println("exported remote object with loader as context class loader");
loader = null;
System.err.println("nulled strong reference to loader");
UnicastRemoteObject.unexportObject(obj, true);
System.err.println("unexported remote object");
/*
* HACK: Work around the fact that the sun.misc.GC daemon thread
* also has this bug-- it will have inherited our loader as its
* context class loader-- by giving it a chance to pass away.
*/
Thread.sleep(2000);
System.gc();
System.err.println("waiting to be notified of loader being weakly reachable...");
Reference dequeued = refQueue.remove(TIMEOUT);
if (dequeued == null) {
System.err.println("TEST FAILED: loader not deteced weakly reachable");
dumpThreads();
throw new RuntimeException("TEST FAILED: loader not detected weakly reachable");
}
System.err.println("TEST PASSED: loader detected weakly reachable");
dumpThreads();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("TEST FAILED: unexpected exception", e);
} finally {
try {
UnicastRemoteObject.unexportObject(obj, true);
} catch (RemoteException e) {
}
}
}
use of java.lang.ref.Reference in project jdk8u_jdk by JetBrains.
the class JpegWriterLeakTest method main.
public static void main(String[] args) {
final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>();
final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>();
int count = 2;
do {
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
final WeakReference<? extends ImageWriter> ref = new WeakReference<>(writer, queue);
refs.add(ref);
try {
final ImageOutputStream os = ImageIO.createImageOutputStream(new ByteArrayOutputStream());
writer.setOutput(os);
writer.write(getImage());
// NB: dispose() or reset() workarounds the problem.
} catch (IOException e) {
} finally {
writer = null;
}
count--;
} while (count > 0);
System.out.println("Wait for GC...");
final long testTimeOut = 60000L;
final long startTime = System.currentTimeMillis();
while (!refs.isEmpty()) {
// check for the test timeout
final long now = System.currentTimeMillis();
if (now - startTime > testTimeOut) {
System.out.println();
throw new RuntimeException("Test FAILED.");
}
System.gc();
try {
System.out.print(".");
Thread.sleep(1000);
} catch (InterruptedException e) {
}
;
Reference<? extends ImageWriter> r = queue.poll();
if (r != null) {
System.out.println("Got reference: " + r);
refs.remove(r);
}
}
System.out.println("Test PASSED.");
}
Aggregations