out of memory error when writing large file

I have the piece of code below which works fine for writing small files, but when it encounters much larger files (>80M), the jvm throws an out of memory error.

I believe it has something to do with the Stream classes. If I were to replace my PrintStream reference with the System.out object (which is commented out below), then it runs fine.

Anyone else encountered this before?

print = new PrintStream(new FileOutputStream(new File(a_persistDir, getCacheFilename()),

false));

//print = System.out;

for(Iterator strings = m_lookupTable.keySet().iterator(); strings.hasNext(); ) {

StringBuffer sb = new StringBuffer();

String string = (String) strings.next();

String id = string;

sb.append(string).append(KEY_VALUE_SEPARATOR);

Collection ids = (Collection) m_lookupTable.get(id);

for(Iterator idsIter = ids.iterator(); idsIter.hasNext();) {

IBlockingResult blockingResult = (IBlockingResult) idsIter.next();

sb.append(blockingResult.getId()).append(VALUE_SEPARATOR);

}

print.println(sb.toString());

print.flush();

}

} catch (IOException e) {

...

} finally {

if( print != null )

print.close();

}

[1251 byte] By [kaneseea] at [2007-11-26 14:30:08]
# 1
There must be more to your code than what you posted, in which you are consuming too much memory yourself. I'm sure the error is not in the PrintStream nor FileOutputStream classes.
warnerjaa at 2007-7-8 2:24:44 > top of Java-index,Java Essentials,Java Programming...
# 2
Does the StringBuffer become very very large? Get rid of the StringBuffer. Instead of this:sb.append(blockingResult.getId()).append(VALUE_SEPARATOR);do this:print.print(blockingResult.getId());print.print(VALUE_SEPARATOR);
sjasjaa at 2007-7-8 2:24:44 > top of Java-index,Java Essentials,Java Programming...
# 3

I will eventually hook up a profiler to it, but I was just wondering if the stream classes could be the cause.

It's just odd that the out of memory occurs during this function call (and a partial file was created) and if I replace the print object reference with the System.out, then it goes through to completion.

kaneseea at 2007-7-8 2:24:44 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes, my first code would just print the strings as I got them. But it was running out of memory then as well. I thought of constructing a StringBuffer first because I was afraid that the PrintStream wasn't allocating the memory correctly.

I've also tried flushing the PrintStream after every line is written but I still run into trouble.

kaneseea at 2007-7-8 2:24:44 > top of Java-index,Java Essentials,Java Programming...