How to avoid OutOfMemoryError when using Bytebuffers and NIO?

I use ByteBuffersand FileChannelswrite binary data to a file. When you do this for large files or sequentially for multiple files, I get an exception OutOfMemoryError. I read elsewhere that use ByteBufferswith NIO is impaired and should be avoided. Have any of you already encountered such a problem and found a solution for efficiently saving large amounts of binary data in a file in java?

Is jvm option possible -XX:MaxDirectMemorySize?

+3
source share
6 answers

, ByteBuffer, . ByteBuffer, , FileChannel. reset ByteBuffer , .

+6

Java Mapped Byte Buffers, " ". , "" . / -, , . NIO, Java, - .

, , :

  • 32- 4 -. ( , 64- .)
  • - JVM, . Sun JVM, , YMMV.

Kirk Pepperdine ( Java) - www.JavaPerformanceTuning.com, MBB: NIO

+4

( , , , ), ;-)

, . java.io.FileOutputStream (.. DataOutputStream, ObjectOutputStream) , , serializeable . .

() . .

+1

. , , , . , , .

0

transferFrom , , , , .

0

JDK.

Sun JVM GC. GC , ByteBuffers . , .

, , JVM . , ByteBuffer SocketChannel -.

The workaround is to use a small number of direct buffers yourself and save them for reuse.

0
source

Source: https://habr.com/ru/post/1696544/


All Articles