I was wondering if it was possible to prevent memory of a object (class or struct) from being swapped to disk?
Edit: As for why I've been told some of the data I'm going to be working with cannot be written to disk.
I dont expect it to be left long enough for the data to be swapped out but thought it's worth checking.
Memory swapping is a memory reclamation method wherein memory contents not currently in use are swapped to a disk to make the memory available for other applications or processes. The exact state or "page" of memory is copied to the disk to make the data contiguous and easy to restore later.
RES -- Resident Memory Size (KiB): The non-swapped physical memory a task has used. SHR -- Shared Memory Size (KiB): The amount of shared memory available to a task, not all of which is typically resident. It simply reflects memory that could be potentially shared with other processes.
From the Advanced tab, click Settings under the Performance heading. From the Advanced tab click Change under the Virtual memory heading. Uncheck the "Automatically manage paging file size for all drives" box. With the drive to disable virtual memory selected in the box, select No paging file.
Swap space is available in Windows and Linux-based operating systems by default. The amount of swap space generally equals twice the RAM of the system.
I am still not clear on why you want to do this. In the context of C#, you have to do two things: "pin" the memory so it cannot be relocated by garbage collection, and then lock it so that it doesn't get swapped out.
Here is a nice blog post that describes how to do the first part (pinning):
http://www.matthew-long.com/2005/10/18/memory-pinning/
Now you need the address and extent of the object to be able to invoke VirtualLock
:
http://msdn.microsoft.com/en-us/library/Aa366895
Note that VirtualLock
only locks pages (units of 4K), so your memory area needs to be at least that large, and aligned to the start of a page. I am assuming that it needs to be invoked in an unsafe
context, though I am not sure.
Previous posting on the topic: Prevent an object from being paged out (VirtualLock equivalent)
Another related blog post: http://geekswithblogs.net/robp/archive/2008/08/13/speedy-c-part-3-understanding-memory-references-pinned-objects-and.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With