java - Memory usage in loading a 226MB text file -


i have read text file of 226mb made this:

0 25 1 1382 2 99 3 3456 4 921 5 1528 6 578 7 122 8 528 9 81 

the first number index, second value. want load vector of short reading file (8349328 positions), wrote code:

    short[] docsofword = new short[8349328];          br2 = new bufferedreader(new filereader("termoccurrenceincollection.txt"));                      ss = br2.readline();         while(ss!=null)         {             docsofword[integer.valueof(ss.split("\\s+")[0])] = short.valueof(ss.split("\\s+")[1]);  //[indexterm] - numoccincollection             ss = br2.readline();         }     br2.close(); 

it turns out entire load takes incredible amount of memory of 4.2gb. don't understand why, expected 15mb vector. answer.

if file generated you, use objectoutputstream, easy way read file.

as @durandal, change code accordingly. giving sample code below.

short[] docsofword = new short[8349328];      br2 = new bufferedreader(new filereader("termoccurrenceincollection.txt"));                  ss = br2.readline();     int strindex, index;     while(ss!=null)     {        strindex = ss.indexof( ' ' );        index = integer.parseint(ss.substr(0, strindex));        docsofword[index] = short.parseshort(ss.substr(strindex+1));        ss = br2.readline();     } br2.close(); 

even can optimise further. instead of indexof() can write our own method, when char matching space, parse string integer. after indexof space , index remain string.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -