提交 f0060634 编写于 作者: N ngmr

7094995: Trailing daemon thread causes continuous GC in agentvm mode

Summary: Shutdown GcInducingThread once test (successfully) finishes
Reviewed-by: alanb, chegar, dholmes, darcy
Contributed-by: NNeil Richards <neil.richards@ngmr.net>
上级 84bd87a5
...@@ -63,11 +63,9 @@ public class ClearStaleZipFileInputStreams { ...@@ -63,11 +63,9 @@ public class ClearStaleZipFileInputStreams {
File.createTempFile("test-data" + compression, ".zip"); File.createTempFile("test-data" + compression, ".zip");
tempZipFile.deleteOnExit(); tempZipFile.deleteOnExit();
ZipOutputStream zos = try (FileOutputStream fos = new FileOutputStream(tempZipFile);
new ZipOutputStream(new FileOutputStream(tempZipFile)); ZipOutputStream zos = new ZipOutputStream(fos)) {
zos.setLevel(compression); zos.setLevel(compression);
try {
for (int i = 0; i < ZIP_ENTRY_NUM; i++) { for (int i = 0; i < ZIP_ENTRY_NUM; i++) {
String text = "Entry" + i; String text = "Entry" + i;
ZipEntry entry = new ZipEntry(text); ZipEntry entry = new ZipEntry(text);
...@@ -78,33 +76,47 @@ public class ClearStaleZipFileInputStreams { ...@@ -78,33 +76,47 @@ public class ClearStaleZipFileInputStreams {
zos.closeEntry(); zos.closeEntry();
} }
} }
} finally {
zos.close();
} }
return tempZipFile; return tempZipFile;
} }
private static void startGcInducingThread(final int sleepMillis) { private static final class GcInducingThread extends Thread {
final Thread gcInducingThread = new Thread() { private final int sleepMillis;
public void run() { private boolean keepRunning = true;
while (true) {
System.gc(); public GcInducingThread(final int sleepMillis) {
try { this.sleepMillis = sleepMillis;
Thread.sleep(sleepMillis); }
} catch (InterruptedException e) { }
public synchronized void run() {
while (keepRunning) {
System.gc();
try {
wait(sleepMillis);
} catch (InterruptedException e) {
System.out.println("GCing thread unexpectedly interrupted");
return;
} }
} }
}; }
gcInducingThread.setDaemon(true); public synchronized void shutDown() {
gcInducingThread.start(); keepRunning = false;
notifyAll();
}
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
startGcInducingThread(500); GcInducingThread gcThread = new GcInducingThread(500);
runTest(ZipOutputStream.DEFLATED); gcThread.start();
runTest(ZipOutputStream.STORED); try {
runTest(ZipOutputStream.DEFLATED);
runTest(ZipOutputStream.STORED);
} finally {
gcThread.shutDown();
gcThread.join();
}
} }
private static void runTest(int compression) throws Exception { private static void runTest(int compression) throws Exception {
...@@ -113,21 +125,16 @@ public class ClearStaleZipFileInputStreams { ...@@ -113,21 +125,16 @@ public class ClearStaleZipFileInputStreams {
System.out.println("Testing with a zip file with compression level = " System.out.println("Testing with a zip file with compression level = "
+ compression); + compression);
File f = createTestFile(compression); File f = createTestFile(compression);
try { try (ZipFile zf = new ZipFile(f)) {
ZipFile zf = new ZipFile(f); Set<Object> refSet = createTransientInputStreams(zf, rq);
try {
Set<Object> refSet = createTransientInputStreams(zf, rq); System.out.println("Waiting for 'stale' input streams from ZipFile to be GC'd ...");
System.out.println("(The test will hang on failure)");
System.out.println("Waiting for 'stale' input streams from ZipFile to be GC'd ..."); while (false == refSet.isEmpty()) {
System.out.println("(The test will hang on failure)"); refSet.remove(rq.remove());
while (false == refSet.isEmpty()) {
refSet.remove(rq.remove());
}
System.out.println("Test PASSED.");
System.out.println();
} finally {
zf.close();
} }
System.out.println("Test PASSED.");
System.out.println();
} finally { } finally {
f.delete(); f.delete();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册