dcsimg
Memory problems BufferedReader/String.substring
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Matias_Jansson
Posted On:   Friday, July 5, 2002 03:06 AM

I have written a simple parser for a text file. Each line in the file corresponds to a record of data. I want to store some (but not all) of the information on each line in an object (one object per line of data). Each line has a fixed number of characters. I am using a BufferedReader and repeatedly call readLine() on this reader. I parse the String returned by readLine() by calling subString() with indices corresponding to the data I am interested in. Example: 2002020512345NAME1234620020501 line = in.readLine(); String date = line.substring(0,8); String name = line.substring(13,17); Investigation has shown that the parser consum   More>>

I have written a simple parser for a text file. Each line in the file corresponds to a record of data. I want to store some (but not all) of the information on each line in an object (one object per line of data). Each line has a fixed number of characters.

I am using a BufferedReader and repeatedly call readLine() on this reader. I parse the String returned by readLine() by calling subString() with indices corresponding to the data I am interested in.

			

Example:

2002020512345NAME1234620020501

line = in.readLine();
String date = line.substring(0,8);
String name = line.substring(13,17);




Investigation has shown that the parser consumes memory 4 times the size of the file. I have an idea that each line gets locked from garbage collection since the sub strings refer to them (since they share the same char[] ). Another idea I have is that each character is doubled in size since char is 2 bytes. My first attempt was to use a StringBuffer like this:
			

line = in.readLine();
sb.replace(0, line.length(), line);
String date = sb.substring(0,8);
String name = sb.substring(13,17);



The intention was to avoid locking line from garbage collection. I could not find a notable difference. I have tried using "-Xrunhprof" but have a hard time interpret the results. Anyone who has any ideas?    <<Less
About | Sitemap | Contact