I am trying to read a lot of data(10k-20k records) from files (10 threads running for 10 mins). I get an exception:
Exception in thread "main" Exception in thread "Thread-26" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
I get the above error message for the code snippet below. I have been trying to debug this : and the closest I have come is to use CharSequence. But I still get the heap exception. (At this point - can anyone help me understand why CharSequence would be better? => It seems it'll load smaller amount of data in main memory, but eventually all the data needs to be in the main memory).
I am able to run the code if for 1 min. But anything near 10 mins explodes. Is there an efficient way to read the files?
**This code is part of a research and I am still re-factoring it, so a lot of inefficient code does exist.
try{
for(int i=0; i<threadCount; i++){
fstream = new FileInputStream(dir+"//read"+machineid+"-"+i + ".txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String line;
// Read File Line By Line
String[] tokens;
while ((line = br.readLine()) != null) {
tokens = line.split(",");
logObject record = new logObject(tokens[0], tokens[1], tokens[2],tokens[3], tokens[4], tokens[5], tokens[6], tokens[7], "", tokens[8]);
toBeProcessed[toBeProcessedArraySz] = record;
toBeProcessedArraySz++;
if(readToValidate == toBeProcessedArraySz){
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace(System.out);
}
//create thread to process the read records
ValidationThread newVThread = new ValidationThread(props,toBeProcessed, updateStats, initCnt, semaphore, finalResults, staleSeqSemaphore, staleSeqTracker, seqTracker, seenSeqSemaphore, toBeProcessedArraySz, freshnessBuckets,bucketDuration);
vThreads.add(newVThread);
toBeProcessedArraySz = 0;
toBeProcessed = new logObject[readToValidate];
semaphore.release();
newVThread.start();
}
}
br.close();//remove to test
fstream.close();
}
}catch(Exception e){
e.printStackTrace(System.out);
}