简体   繁体   中英

How does Native memory related to Java gets cleared. I understand the GC doesnot clears it

Seems like when there is IO operations waiting for external service lots of thread are waiting at sun.misc.Unsafe.park. This contributed to native memory buildup that does not gets cleared. How does this Native memory gets cleared. Does it gets cleared after some time automatically

Using thread-per-connction IO incurs the following memory costs:

  1. thread stack
  2. ByteBuffer or byte[] passed to the read/write/send/receive method
  3. potentially a bounce buffer (an openjdk implementation detail) since the syscalls expect a fixed buffer while the java data structures can be moved by the GC
  4. additional abstractions and application on top of the IO functions kept alive for the lifetime of the connection
  5. kernel socket memory

  1. can be tweaked a little by adjusting the -Xss argument. A complete fix requires multiplexing multiple connections on fewer thread via nonblocking/asynchronous IO. AsynchronousSocketChannel makes this easy to use for TCP sockets, HttpClient for HTTP. For other protocols you'll need to look for libraries implementing async IO.
  2. is unavoidable, but depending on application design there may be room for optimization. eg if you were to send a 1GB file you don't need a 1GB buffer, you could read it chunk by chunk with smaller buffers
  3. can be avoided by passing DirectByteBuffer instances to IO methods. using async IO also reduces the total number of buffers that need to be used concurrently
  4. not specific to IO, thus general memory footprint optimization advice applies
  5. generally shouldn't be an issue since the kernel auto-sizes them. but of course keeping thousands of sockets open longer than necessary will still have an impact

Overall the JVM should clean up the native resources needed for IO eventually, but when that happens may depend on how long you keep the java objects that depend on those native resources alive.

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