input-output-hk / iodb

Multiversioned key-value database, especially useful for blockchain
Creative Commons Zero v1.0 Universal
95 stars 13 forks source link

Too many mmaped files #2

Closed jankotek closed 7 years ago

jankotek commented 7 years ago

IODB fails with out of memory exception followed by JVM crash:

Caused by: java.lang.OutOfMemoryError: Map failed
    at sun.nio.ch.FileChannelImpl.map0(Native Method)
    at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:937)
    ... 17 more
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to deallocate stack guard pages failed.
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00007fd92aac1000, 12288, 0) failed; error='Cannot allocate memory' (errno=12)

Problem is that too many files are memory mapped. Here is a way to diagnose it:

I have seen these error before when running out of resources such as running out of swap space or running out of allowed memory mapping. Have a look at sudo cat /proc/$PID/maps | wc -l compared with cat /proc/sys/vm/max_map_count

Default limit is 64K mmaps per process. Total number of files in folder was 400 at crash, there is single mmap per file. Mmap handle is released when DirectByteBuffer is GCed, so the GC does not collect buffers fast enough.

Solution is to unmap buffers using cleaner hack from MapDB.