2

I use a GZIPOutputStream with ByteArrayOutputStream to write compressed data to a string. Later on, when I am done with that string, I need to reset this stream to use it with some other string. One way is to close this stream, and create a new one. But I guess, it would be more efficient if I keep using the same stream. But to do that, I need to reset it first. How can I do that?

pythonic
  • 20,589
  • 43
  • 136
  • 219
  • I believe this is an usefull link for you http://stackoverflow.com/questions/3640080/force-flush-on-a-gzipoutputstream-in-java – Rowan Klein Gunnewiek May 05 '17 at 12:59
  • 1
    Why do you think it will be "more efficient"? Just throw them away and create a new one. Java is very efficient at creating new objects. There's also the issue of the internal state - who knows what attempting to "reuse" such objects will do. – Bohemian May 05 '17 at 13:00
  • 1
    I am a bit wary of JVM's garbage collection. I have the opinion that creating more objects will trigger the garbage collector more often, which I want to avoid obviously. Especially, when I have to reset this stream quite often in my program. – pythonic May 05 '17 at 13:01
  • Donald Knuth: _"Premature optimization is the root of all evil (or at least most of it) in programming."_ – vanje May 05 '17 at 13:06
  • 1
    For how long java's garbage collectors have been around you should not worry about them. Write your program as if they are perfect and make changes if there is a problem. the GC can handle millions of object creation/dereferencing in short order before stalling. – markbernard May 05 '17 at 13:19
  • My app handles large amount of data, 10s of GBs that is. In that case, garbage collection overhead is indeed a concern. – pythonic May 05 '17 at 14:00

0 Answers0