Hi, I have a process running for a long time during which i need to log some information about the process to some file. I am not supposed to use new BufferedWriter(new FileWriter(filename)) because i should not keep the file opened during the process. I need a way to store log info in some stream/sink and write it to the file only at the end of the process. This way I can avoid having the file opened during the process. I tried using StringWriter to store the log and finally writing it to the file as follows: FileWriter file = new FileWriter(filename); file.write(mystringwriter.toString()); where mystringwriter is the StringWriter. This gives OutOfMemory error becos the StringBuffer it uses internally exceeds its capacity. Could anyone suggest any ideas on this. Regards, shankar
Is the program really running out of heap, or is it hitting some other limit on the size of the buffer of a StringBuffer? If the latter, I am interested to hear, because I did not know of any such limit. If there is a limit on the size of the buffer in a StringBuffer, but your data will fit in memory, then you could change the structure in which you store it. Perhaps, an ArrayList of Strings (one per item of log) would help you. If you really are running out of heap, and cannot just allocate more heap (via the -Xmx switch on the java command, for instance), then it is more difficult to offer a solution. Compression, via a DeflaterOutputStream, for instance, might gain you something, as logging text is likely to be highly compressible. Otherwise, you are going to have to write your data to file somewhere, because it just plain won't fit in memory. You could, as an earlier respondent has suggested, write to your log file in short bursts, if it is acceptable for you to open the log file at all. If you cannot open the log file at all, you could write to a different temporary log file, then copy its contents to the real log file, later. Copying is quickest via RandomAccessFile, rather than any type of stream, by the way. There are pre-written log packages available. One, I think, is Log4J.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.