diff --git a/CHANGES.txt b/CHANGES.txt index 20c3ca70cf1..96cf00ef92b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -304,7 +304,10 @@ Bug fixes 17. A single hit using the expert level sorted search methods resulted in the score not being normalized. (Yonik Seeley, LUCENE-462) - + +18. Fixed inefficient memory usage when loading an index into RAMDirectory. + (Volodymyr Bychkoviak via Bernhard, LUCENE-475) + Optimizations 1. Disk usage (peak requirements during indexing and optimization) diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.java index 4d0728573bd..a18abe45d42 100644 --- a/src/java/org/apache/lucene/store/RAMDirectory.java +++ b/src/java/org/apache/lucene/store/RAMDirectory.java @@ -53,6 +53,7 @@ public final class RAMDirectory extends Directory { private RAMDirectory(Directory dir, boolean closeDir) throws IOException { final String[] files = dir.list(); + byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE]; for (int i = 0; i < files.length; i++) { // make place on ram disk IndexOutput os = createOutput(files[i]); @@ -60,9 +61,14 @@ public final class RAMDirectory extends Directory { IndexInput is = dir.openInput(files[i]); // and copy to ram disk int len = (int) is.length(); - byte[] buf = new byte[len]; - is.readBytes(buf, 0, len); - os.writeBytes(buf, len); + int readCount = 0; + while (readCount < len) { + int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? len - readCount : BufferedIndexOutput.BUFFER_SIZE; + is.readBytes(buf, 0, toRead); + os.writeBytes(buf, toRead); + readCount += toRead; + } + // graceful cleanup is.close(); os.close();