Can you stop memory from being swapped to disk?

后端 未结 7 1007
北恋
北恋 2021-01-02 02:59

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

7条回答
  •  渐次进展
    2021-01-02 03:37

    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

提交回复
热议问题