简体   繁体   中英

write (byte[] b) optim usage for large byte array

If I have a large byte array already in memory received from a SOAP response.

I have to write this byte array into an OutputStream.

It is OK just to use write:

byte [] largeByteArray=...;

outputstream.write(largeByteArray);

...
outputstream.flush();
...

or is better to split the bytearray in small chunks and to write that to the outputstream?

If you've already got the large array, then just write it out - if the output stream implementation chooses to chunk it, it can make that decision. I can't see a benefit in you doing that for it - which may well make it less efficient, if it's able to handle large chunks.

If you want to make this more efficient, I would write the data as you get it rather than building a large byte[] (and waiting until the end to start writing). If this is an option is it can be faster and more efficient. However if this is not an option use one large write.

What type of output stream are you using? There are output streams that can write the array in chunks. In general I believe if you issue an I/O operation (write) for each single byte, the performance may be poor, because I/O operations are expensive.

I can think of no conceivable reason it would be better without getting truly bizarre and absurd. Generally, if you can pass data between layers in larger chunks without additional effort, then you should do so. Often, it's even worth additional effort to do things that way, so why would you want to put in extra effort to make more work?

If largeByteArray is something really large , and write job cost long time , and memory is a considerable condition:

  • Split the array to parts, after write one part ,set the part=null, this release the reference of the part , would make the JVM/GC the part as soon as possible .

  • By split and release , you can do more write(largeByteArray) job at the same time, before OOM-ERROR occurs.

  • Notice: during split stage, JVM need double arraysize memory to do so, but after split, original array will eventually get GC'd ,you are back to using the same amount of memory as before.

  • Example: a server has 1GB memory . it can run max 100 thread that each one holding and sending 10MB data to client sametime.

    if you use the big 10MB array, memory use is always 1GB,no spare part even all thread has 1MB data not sent.

    my solution is split 10MB to 10*1MB. after send some MB part , the sent part maybe get JVM/GC .and each thread is costing less average memory in whole life time. so server may run more tasks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM