Search code examples
clinuxsolarismmapdiskspace

suspending an application preemptively before out of memory causes a segmentation fault


I have a large application, that allocates large amounts of memory using malloc and mmap I want to trap all methods of failure and try and recover. To cover the case of running out of swap space, I check returns of malloc and realloc and if they're null, the application can prompt the user to clear some disk space before continuing.

The application also allocates many large data arrays using mmap, using sparse files. I wish to be able to recover from situations where a write to mapped memory fails due to disk space limitations. Is there any way of doing this, or am I better off incorporating disk space monitoring and halting the relevant threads in this scenario.

Im working in c, and the application needs to run on linux and solaris


Solution

  • There is no way for a process to anticipate the fact a memory access will fail due to virtual memory shortage, especially with the mmaped sparse file technique you are using.

    You might trap the sigsegv but there are issues with this approach (see Segmentation fault handling ). Otherwise, the simpler solution would be to monitor the file system available space and freeze/stop the most consuming process preventively.