csteifel
csteifel

Reputation: 2934

Keep memory item in RAM, disallow it from being paged to disk

Is there anyway to tell the computer to keep an item in ram or at least to try its hardest to keep it from being paged to disk? I would like to be able to do this to ensure that my section of memory will never have to go to disk to fetch its information as I know it is something that will need to be fetched almost instantly but may not be fetched frequently

This is with linux. Though I would be interested in other platforms if you have an answer for those as well

Upvotes: 3

Views: 498

Answers (3)

Joe
Joe

Reputation: 7808

It's O/S specific, however...

In POSIX world you use mlock and mlockall to indicate that the data must be kept in RAM and not paged out. You may require serious user privileges for this.

Watch out for Linuxes default overcommit policy though. If you want to be sure you actually have the memory you mallocated make sure you touch it all before you lock it and assume it's all in RAM.

Depending on why you really care about this you may even be in the position to remove swap altogether. Depending where your system is swapping to (DMA, PIO etc.) this might be your only assurance of performance, regardless of what you do in your application.

Upvotes: 6

MSalters
MSalters

Reputation: 179907

The proper call is madvise(p, sizeof(*p), MADV_WILLNEED). That tells Linux that you will need *p soon. That will even cause Linux to unpage it, if it accidentily happened to be paged.

Unlike mlock, this call can be done without serious user privileges.

Upvotes: -1

Shivam
Shivam

Reputation: 2132

You can turn off paging within OS. At program level paging is mostly opaque. You can compile your program with 64 bit to get you extra addressable memory.

Upvotes: 0

Related Questions