How to cache external Javascript files?

In Linux, is it possible to mmap() a file in such a way that it will never be swapped out to a disk in favor of files which, using the normal read(), have been placed into the page/buffer cache?

  • If not using mmap(), it seems like there should be a way to give certain files "priority", so that the only time they're swapped out is for page faults trying to bring in, e.g., executing code, or memory that was malloc()'d by some process. One can think of situations where this could be useful. Consider search engines, which should keep their index files in cache. EDIT: Specifically, the mmap()'d file should still be pageable, just not in favor of other files. In the example, the search index files could be enormous, so using mlock() is out of the question unfortunately.

  • Answer:

    There is an mlock(2) call for this specific purpose: to lock region of memory into physical memory. See [1] Second, to lock something into memory, it has to be a virtual memory region. It must be allocated by mmap, malloc or similar calls, but not read(2). Read(2) requires a pre-allocated memory region to be passed in, which is either malloc-ed / mmap-ed, or automatically allocated on the stack. [1] http://man7.org/linux/man-pages/man2/mlock.2.html

Ryan Gao at Quora Visit the source

Was this solution helpful to you?

Other answers

madvise with MADV_WILLNEED may give the area a priority over other system cache pages, but I'm not sure how it is implemented in Linux.

Alexander Lukyanov

Just Added Q & A:

Find solution

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.