java - Storing file contents to StringBuilder apparently takes too much memory -
i have code:
public static void main(string[] args) { system.out.println("reading file..."); string content = readfile(args[0]); system.out.println("done reading file."); } private static string readfile(string file) throws ioexception { bufferedreader reader = new bufferedreader( new filereader (file)); string line = null; stringbuilder stringbuilder = new stringbuilder(); while( ( line = reader.readline() ) != null ) { stringbuilder.append( line ); } return stringbuilder.tostring(); }
the readfile
method works fine, well, small files.
the thing noticed takes memory.
if open system monitor on windows (ctrl-shift-esc), see java process taking 1,8gb ram, while size of file 550mb.
yes, know, loading file entirely memory isn't idea, i'm doing curiosity.
the program gets stuck @ reading file...
when newly created java process starts, takes bunch of mb of ram , goes 1,8gb.
i tried using string concatenation instead of using stringbuilder
, have exact same result.
why take memory? final stringbuilder.tostring
causing this?
you have remember how these libraries work.
one byte on disk can turn 2 byte char. stringbuilder grows doubling in capacity can twice large need, , need both stringbuilder , string in memory @ same time.
so take example. 550 mb can turn 1100 mb char
alone. however, size doubles in size approximately next power of 2 i.e. 2 gb, , on top of string 550 mb.
note: reason not using memory have bug. discarding new lines \r\n
means have less characters.
when processing large file don't have enough memory load memory @ once, better off processing data read it.
btw if have plenty of memory, can read file faster, less memory way.
static string readfile(string file) throws ioexception { try(fileinputstream fis = new fileinputstream(file)) { byte[] bytes = new byte[(int) fis.available()]; fis.read(bytes); return new string(bytes); } }
Comments
Post a Comment