avoid File.deleteOnExit() on temp files

JVM registers each file in a list regardless of already deleted and never
cleans up the list during runtime.
This accumulates to a considerable amount of mem during large crawls and/or
long uptime.
To tackle this, all temp files are now created in a subdir of java.io.tmpdir 
and the jvm tmpdir property is set to this subdir, which is deleted by
code on shutdown.
Additionally let pdfParser use this tmp subdir too.
pull/27/head
reger 9 years ago
parent 02e4489a23
commit 7d0d19cb8e

@ -74,7 +74,6 @@ public class apkParser extends AbstractParser implements Parser {
Document[] docs = null; Document[] docs = null;
try { try {
File tempFile = File.createTempFile("apk" + System.currentTimeMillis(), "jar"); File tempFile = File.createTempFile("apk" + System.currentTimeMillis(), "jar");
tempFile.deleteOnExit();
final FileOutputStream out = new FileOutputStream(tempFile); final FileOutputStream out = new FileOutputStream(tempFile);
int read = 0; int read = 0;
final byte[] data = new byte[1024]; final byte[] data = new byte[1024];

@ -109,8 +109,7 @@ public class audioTagParser extends AbstractParser implements Parser {
f = AudioFileIO.read(location.getFSFile()); f = AudioFileIO.read(location.getFSFile());
} else { } else {
// create a temporary file, as jaudiotagger requires a file rather than an input stream // create a temporary file, as jaudiotagger requires a file rather than an input stream
tempFile = File.createTempFile(filename,fileext); tempFile = File.createTempFile(filename,fileext);
tempFile.deleteOnExit();
fout = new BufferedOutputStream(new FileOutputStream(tempFile)); fout = new BufferedOutputStream(new FileOutputStream(tempFile));
int c; int c;
while ((c = source.read()) != -1) { while ((c = source.read()) != -1) {

@ -82,7 +82,6 @@ public class bzipParser extends AbstractParser implements Parser {
final BZip2CompressorInputStream zippedContent = new BZip2CompressorInputStream(source); final BZip2CompressorInputStream zippedContent = new BZip2CompressorInputStream(source);
tempFile = File.createTempFile("bunzip","tmp"); tempFile = File.createTempFile("bunzip","tmp");
tempFile.deleteOnExit();
// creating a temp file to store the uncompressed data // creating a temp file to store the uncompressed data
final FileOutputStream out = new FileOutputStream(tempFile); final FileOutputStream out = new FileOutputStream(tempFile);

@ -80,7 +80,6 @@ public class gzipParser extends AbstractParser implements Parser {
final GZIPInputStream zippedContent = new GZIPInputStream(source); final GZIPInputStream zippedContent = new GZIPInputStream(source);
tempFile = File.createTempFile("gunzip","tmp"); tempFile = File.createTempFile("gunzip","tmp");
tempFile.deleteOnExit();
// creating a temp file to store the uncompressed data // creating a temp file to store the uncompressed data
final FileOutputStream out = new FileOutputStream(tempFile); final FileOutputStream out = new FileOutputStream(tempFile);

@ -229,7 +229,6 @@ public class odtParser extends AbstractParser implements Parser {
try { try {
// creating a tempfile // creating a tempfile
dest = File.createTempFile("OpenDocument", ".odt"); dest = File.createTempFile("OpenDocument", ".odt");
dest.deleteOnExit();
// copying the stream into a file // copying the stream into a file
FileUtils.copy(source, dest); FileUtils.copy(source, dest);

@ -215,7 +215,6 @@ public class ooxmlParser extends AbstractParser implements Parser {
try { try {
// creating a tempfile // creating a tempfile
dest = File.createTempFile("OpenDocument", ".odt"); dest = File.createTempFile("OpenDocument", ".odt");
dest.deleteOnExit();
// copying the stream into a file // copying the stream into a file
FileUtils.copy(source, dest); FileUtils.copy(source, dest);

@ -63,6 +63,7 @@ import net.yacy.document.VocabularyScraper;
import net.yacy.kelondro.io.CharBuffer; import net.yacy.kelondro.io.CharBuffer;
import net.yacy.kelondro.util.FileUtils; import net.yacy.kelondro.util.FileUtils;
import net.yacy.kelondro.util.MemoryControl; import net.yacy.kelondro.util.MemoryControl;
import org.apache.pdfbox.pdfparser.PDFParser;
public class pdfParser extends AbstractParser implements Parser { public class pdfParser extends AbstractParser implements Parser {
@ -100,13 +101,13 @@ public class pdfParser extends AbstractParser implements Parser {
// create a pdf parser // create a pdf parser
PDDocument pdfDoc; PDDocument pdfDoc;
//final PDFParser pdfParser;
try { try {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // the pdfparser is a big pain Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // the pdfparser is a big pain
pdfDoc = PDDocument.load(source); //pdfDoc = PDDocument.load(source);
//PDFParser pdfParser = new PDFParser(source); final PDFParser pdfParser = new PDFParser(source);
//pdfParser.parse(); pdfParser.setTempDirectory(new File(System.getProperty("java.io.tmpdir")));
//pdfDoc = pdfParser.getPDDocument(); pdfParser.parse();
pdfDoc = pdfParser.getPDDocument();
} catch (final IOException e) { } catch (final IOException e) {
throw new Parser.Failure(e.getMessage(), location); throw new Parser.Failure(e.getMessage(), location);
} finally { } finally {

@ -819,7 +819,6 @@ public final class FileUtils {
File.createTempFile( File.createTempFile(
parserClassName + "_" + ((idx > -1) ? fileName.substring(0, idx) : fileName), parserClassName + "_" + ((idx > -1) ? fileName.substring(0, idx) : fileName),
(!fileExt.isEmpty()) ? "." + fileExt : fileExt); (!fileExt.isEmpty()) ? "." + fileExt : fileExt);
tempFile.deleteOnExit();
return tempFile; return tempFile;
} }

@ -832,7 +832,6 @@ public final class SeedDB implements AlternativeDomainNames {
try { try {
// create a seed file which for uploading ... // create a seed file which for uploading ...
seedFile = File.createTempFile("seedFile",".txt", seedDB.myOwnSeedFile.getParentFile()); seedFile = File.createTempFile("seedFile",".txt", seedDB.myOwnSeedFile.getParentFile());
seedFile.deleteOnExit();
if (Network.log.isFine()) Network.log.fine("SaveSeedList: Storing seedlist into tempfile " + seedFile.toString()); if (Network.log.isFine()) Network.log.fine("SaveSeedList: Storing seedlist into tempfile " + seedFile.toString());
final ArrayList<String> uv = storeSeedList(seedFile, true); final ArrayList<String> uv = storeSeedList(seedFile, true);

@ -128,6 +128,7 @@ public final class yacy {
* @param startupFree free memory at startup time, to be used later for statistics * @param startupFree free memory at startup time, to be used later for statistics
*/ */
private static void startup(final File dataHome, final File appHome, final long startupMemFree, final long startupMemTotal, final boolean gui) { private static void startup(final File dataHome, final File appHome, final long startupMemFree, final long startupMemTotal, final boolean gui) {
String tmpdir=null;
try { try {
// start up // start up
System.out.println(copyright); System.out.println(copyright);
@ -202,6 +203,12 @@ public final class yacy {
lock = channel.tryLock(); // lock yacy.running lock = channel.tryLock(); // lock yacy.running
} catch (final Exception e) { } } catch (final Exception e) { }
// set jvm tmpdir to a subdir for easy cleanup (as extensive use file.deleteonexit waists memory during long runs, as todelete files names are collected and never cleaned up during runtime)
try {
tmpdir = java.nio.file.Files.createTempDirectory("yacy-tmp-").toString(); // creates sub dir in jvm's temp (see System.property "java.io.tempdir")
System.setProperty("java.io.tmpdir", tmpdir);
} catch (IOException ex) { }
try { try {
sb = new Switchboard(dataHome, appHome, "defaults/yacy.init".replace("/", File.separator), conf); sb = new Switchboard(dataHome, appHome, "defaults/yacy.init".replace("/", File.separator), conf);
} catch (final RuntimeException e) { } catch (final RuntimeException e) {
@ -386,6 +393,8 @@ public final class yacy {
} finally { } finally {
} }
if (tmpdir != null) FileUtils.deletedelete(new File(tmpdir)); // clean up temp dir (set at startup as subdir of system.propery "java.io.tmpdir")
ConcurrentLog.config("SHUTDOWN", "goodbye. (this is the last line)"); ConcurrentLog.config("SHUTDOWN", "goodbye. (this is the last line)");
ConcurrentLog.shutdown(); ConcurrentLog.shutdown();
shutdownSemaphore.release(1000); shutdownSemaphore.release(1000);
@ -618,7 +627,7 @@ public final class yacy {
if (OS.isWindows) headless = false; if (OS.isWindows) headless = false;
if (args.length >= 1 && args[0].toLowerCase().equals("-gui")) headless = false; if (args.length >= 1 && args[0].toLowerCase().equals("-gui")) headless = false;
System.setProperty("java.awt.headless", headless ? "true" : "false"); System.setProperty("java.awt.headless", headless ? "true" : "false");
String s = ""; for (final String a: args) s += a + " "; String s = ""; for (final String a: args) s += a + " ";
yacyRelease.startParameter = s.trim(); yacyRelease.startParameter = s.trim();

Loading…
Cancel
Save