Should I use memory-mapped files aggressively instead of heap on server applications?
-
My server application typically deals with transient memory allocations of blobs (binary objects) ranging from 1 MB - 300 MB. These blobs are uncompressed, processed (modified), then re-compressed. The entire process took less than a fraction of a second on modern CPUs. I also have a phobia of a condition called "out of memory". My rationale is this: when the application process runs out of memory, there is a short moment of time where every thread and every module will see that new memory allocations fail. My application depends on some third-party modules and source code of various level of robustness, so it's not guaranteed that all modules/threads could cleanly back-track from an allocation failure. The way I deal with memory-mapped file is that I only map in the section of memory that I'm currently using. I see it as a hint to the OS that when I unmap a section of the file, that I'm not going to use that section for the next moment, so the OS could page that out if it wish to. Is my understanding correct? Or was it wishful-thinking and just an abuse of the OS memory system?
-
Answer:
Instead of mmap/munmap, you can use mlock/munlock on the required portions of the file. munmap will move the pages to Inactive - by decreasing reference count - thus making them first candidates to be gobbled up during memory pressure. If you use mlock/munlock, pages are still active. So they are not the immediate candidates for reclamation during memory pressure. This may help if you application needs to revisit those portions again in near future.
Anand Bhat at Quora Visit the source
Related Q & A:
- How do I use my nickname instead of my real name on messenger?Best solution by Yahoo! Answers
- How do I use my own picture instead of an avatar like on the old messenger?Best solution by Yahoo! Answers
- How can I use my own picture instead of the avatar??Best solution by Yahoo! Answers
- How do I use a picture instead of my avatar for chat?Best solution by Yahoo! Answers
- Can I use my Philippine passport instead of a US passport?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.