FearedAtheist
10-12-2010, 02:51 PM
I'm using the following function to read in a (currently 1.3 mb XML file) and return it to a socket for writing to a remote PHP script. When the function appends the buffer (tempData) to the String containing the file data (data), my memory usage spikes up to 1.1 gigs. I knew Java was a memory hog but I just HAVE to be doing something wrong.
Alternatively, I've tried reading the file using a char array. It's much faster, but as I eventually have to convert it to a String, I still encounter the same memory problem.
Am I working too high level with this amount of data? Do I need to start thinking bytes [] ? I can't understand where Java comes up with 1.1 gigs...
Thanks for any tips or suggestions.
public static String getPlaylist(String path) {
//open file
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
} catch (IOException e) {
System.out.println("Couldn't create file reader!");
System.exit(-1);
}
String data = "";
try {
String tempData = null;
while ((tempData = in.readLine()) != null) {
data += tempData;
}
} catch (IOException e) {
System.out.println("Couldn't read file!");
System.exit(-1);
}
return data;
}
Alternatively, I've tried reading the file using a char array. It's much faster, but as I eventually have to convert it to a String, I still encounter the same memory problem.
Am I working too high level with this amount of data? Do I need to start thinking bytes [] ? I can't understand where Java comes up with 1.1 gigs...
Thanks for any tips or suggestions.
public static String getPlaylist(String path) {
//open file
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
} catch (IOException e) {
System.out.println("Couldn't create file reader!");
System.exit(-1);
}
String data = "";
try {
String tempData = null;
while ((tempData = in.readLine()) != null) {
data += tempData;
}
} catch (IOException e) {
System.out.println("Couldn't read file!");
System.exit(-1);
}
return data;
}