Skip to content

Commit

Permalink
some better memory protection and OOM prevention in EcoFS
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5523 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Jan 26, 2009
1 parent e76cbd9 commit 3154926
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions source/de/anomic/kelondro/kelondroEcoFS.java
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.io.RandomAccessFile;

import de.anomic.server.serverMemory;

/**
* The EcoFS is a flat file with records of fixed length. The file does not contain
* any meta information and the first record starts right at file position 0
Expand Down Expand Up @@ -65,8 +67,8 @@ public class kelondroEcoFS {
/**
* stay below hard disc cache (is that necessary?)
*/
private static final int maxReadCache = 512 * 1024;
private static final int maxWriteBuffer = 512 * 1024;
private static final int maxReadCache = 256 * 1024;
private static final int maxWriteBuffer = 256 * 1024;


public kelondroEcoFS(final File tablefile, final int recordsize) throws IOException {
Expand Down Expand Up @@ -99,8 +101,19 @@ public kelondroEcoFS(final File tablefile, final int recordsize) throws IOExcept
}

// initialize cache and buffer
cache = new byte[Math.max(1, (maxReadCache / recordsize)) * recordsize];
buffer = new byte[Math.max(1, (maxWriteBuffer / recordsize)) * recordsize];
int cachesize = Math.max(1, (maxReadCache / recordsize)) * recordsize;
int buffersize = Math.max(1, (maxWriteBuffer / recordsize)) * recordsize;
if (!serverMemory.request(cachesize + buffersize + 1024 * 1024 * 20, true)) {
// not enough memory there, take less
long lessmem = Math.min(Math.min(maxReadCache, maxWriteBuffer) / 8, serverMemory.available() - (1024 * 1024 * 6) / 6);
//System.out.println("newmem vorher: cachesize = " + cachesize + ", buffersize = " + buffersize + ", available = " + serverMemory.available() + ", lessmem = " + lessmem);
cachesize = Math.max(1, (int) (lessmem / recordsize)) * recordsize;
buffersize = Math.max(1, (int) (lessmem / recordsize)) * recordsize;
//System.out.println("newmem nachher: cachesize = " + cachesize + ", buffersize = " + buffersize);
}

cache = new byte[cachesize];
buffer = new byte[buffersize];
this.buffercount = 0;

// first-time read of cache
Expand Down

0 comments on commit 3154926

Please sign in to comment.