• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will the following code run out of heap space ?.  RSS feed

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experts,

We are doing bunch of file writes using the following code, basically the following code snippet, accepts an inputstream and writes it to the disk.



I have few questions:

If the above operation, is done few thousand times on multiple threads, will this cause a Heap Java.OutofMemory error ?

Please suggest a better way of writing an input stream to a file.

Thanks a bunch.
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Rodeo:
If the above operation, is done few thousand times on multiple threads, will this cause a Heap Java.OutofMemory error ?

Most likely, yes. Why? Because you never close your streams. You should close output in a finally block.

It's also always a bad idea to ignore exceptions - the least you should do is print or log them.

Please suggest a better way of writing an input stream to a file.

Use the read / write methods that take an array:

Flushing in between calls may also improve performance as far as memory is concerned.


Just another hint too: if you are writing to the same disk in parallel it will often be slower than sequentially. I have had multiple experiences of two consecutive copy processes both slowing down; after stopping one the other's speed rose quite drastically. The reason is that there is less need for repositioning of the disk write head.
[ June 19, 2008: Message edited by: Rob Prime ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!