Virtual Memory

Started by
18 comments, last by frob 6 years, 9 months ago

I'm using C#, and trying to test whether Windows has virtual memory enabled or not.  I just need a function that can return a bool for this.  Does anyone know of one?  I know it's possible because I remember trying to play a game once and getting an error that said I needed to enable virtual memory.

Advertisement

You can't disable virtual memory, but you can change the size of the pagefile. Note that even with a 0-sized pagefile, virtual memory is still enabled. 

Try searching for how to read the pagefile size, e.g. https://stackoverflow.com/questions/15518774/total-page-file-size-of-my-computer-using-c-sharp

However.... why are you doing this? There should be no need.

4 hours ago, Hodgman said:

You can't disable virtual memory,

Can't I?  Then what's this option called "No paging file"?

Anyway, the reason I want to check for this is because I want my program to only run if virtual memory is disabled (or I guess if the paging file is 0 bytes), otherwise it will give an error and exit.

I couldn't edit any more for some reason because I guess they imposed a stupid space limitation.

Anyway, come to think of it, then how did the game know whether I had virtual memory enabled or not?

Edit on not noticing the C# tag :o
c# wise Im pretty sure something along the lines of (https://msdn.microsoft.com/en-us/library/system.diagnostics.process.pagedmemorysize64(v=vs.110).aspx) that is what you would want

 

In terms of wanting to check the maximum allowed virtual memory / pagefile size your application is allowed (https://msdn.microsoft.com/en-us/library/windows/desktop/aa366589(v=vs.85).aspx) that is a link to the GlobalMemoryStatus which returns a struct that has that info available in it.

it wont be able to show if its "enabled" or not, since it always is enabled.. you just change the size, but im guessing the game called something similar to that and then had a condition of if the size is 0 .... much like what you are wanting to do
 

Correct me if I'm wrong (and I may well be) but the function in the first link actually seems to tell me how much virtual memory the program/process is actually using.  I only want to know the total size the the entire swap file or whatever you want to call it.  As in, how much is allocated for the whole computer to be allowed to use at maximum.

 

But the way you described it, it seems like that's what the second link is doing?  Did you accidentally say it backwards?  And as for the structure mentioned in the second link, I looked up the ullTotalPageFile, which seems like it should be for the whole system, but according to a link I followed from there, it seems to be for just the current process, so maybe that's an accurate description after all.

 

In any case, the only thing I need is the whole maximum amount set for virtual memory for the whole system (the "Maximum size (MB)" in the virtual memory properties window).  So whatever way I can get that would be great!  I prefer not to have to use a DLL but I will if I must.  Thanks a lot.

And the number needs to exclude the size of RAM.

1 hour ago, myvraccount said:

Can't I?  Then what's this option called "No paging file"?

That disables demand paging not virtual memory... virtual memory is used to protect each applications memory space from each other.  While demand paging allows you to use more memory than physically available.

-potential energy is easily made kinetic-

2 hours ago, myvraccount said:

Can't I?  Then what's this option called "No paging file"?

 

Virtual memory, the general computing concept, is about providing a mapping to actual physical memory addresses from a (potentially larger) virtual (that is, "pretend") address space. This is a core OS feature in just about every modern operating system, and cannot be disabled. It is the thing that lets you (a) have an object at "address X" while another process also thinks it has a wholly different object at "address X," or allow both processes to think they're loading at the same address in memory when obviously they cannot be and (b) address the entire range of the address space even though you don't actually have that address space physically, assuming you don't address it all at once. It also provides other benefits, like memory protection via isolation.

You can't turn this feature off.

Colloquially, "virtual memory" is sometimes used in the context of Windows to refer to the page file and its size. This is a bit of a misnomer, as the use of a page file in the implementation of virtual memory is an optional implementation detail. However, since the Windows control panel for controlling the page file puts it under the "virtual memory" heading, and there are no other controls in that section, it's a term that kinda sticks. The page file is a mechanism to allow the OS to set aside memory that is in-use (but not immediately in use right now) on the disk in order to make room in physical memory and thus preserve the illusion of a larger virtual memory space.

So what you seem to be asking for is detecting the size of the page file. You probably cannot do this exactly the way you want, but if you call GetPerformanceInfo, you can read the CommitLimit member of the PERFORMANCE_INFO structure it fills out. This will tell you the global limit on pages that can be committed by the system without causing the page file to grow. You can multiply this by the size of a page file ("PageSize") to get a value in bytes. This will include physical RAM, which you'd need to subtract out potentially, depending on what you're doing. It's also not going to report what the page file is allowed to grow to.

2 hours ago, myvraccount said:

Anyway, come to think of it, then how did the game know whether I had virtual memory enabled or not?

There's a good chance they didn't, since they used the term "virtual memory." Now, perhaps they picked that term because of the reason I outlined above, where it's fallen into colloquial use among Windows users as a synonym of the page file, and they wanted to make sure users understood what exactly was "enabled." 

But my bet would be that they just made some calls into GetPerformanceInfo or GlobalMemoryStatusEx, read some numbers from it, maybe did some subtraction or other potentially-fuzzy math, and determined based on that whether or not the page file was enabled. I would not by default trust that the wording of that dialog means you can perform precisely the check you're thinking about performing.

Why do you only want your program to run if the page file is disabled, exactly?

jpetrie, thanks for the info.  It's enlightening.  I was under the impression that what you're referring to a virtual memory, in terms of address re-mapping, was actually called protected mode addressing (at least that's what they called it when I studied assembly language for 68000 processors), but supposedly there are "rings" on PC, and the lower ones use that.  I don't know.

 

Anyway, the reason I do not want the program running if virtual memory (or demand paging, or whatever it's really called) is enabled (or is greater than 0 bytes for maximum size), is because it's a huge security flaw!  If a program encrypts anything before saving, then un-encrypted data will never exist on the hard drive, but if this mechanism can arbitrarily save anything in RAM onto the hard drive, that's very dangerous!

 

Also, I'm inclined to believe that there must be some way to do it, because after all, Windows keeps track of that information, so it must be stored somewhere right?

10 minutes ago, myvraccount said:

Anyway, the reason I do not want the program running if virtual memory (or demand paging, or whatever it's really called) is enabled (or is greater than 0 bytes for maximum size), is because it's a huge security flaw!  If a program encrypts anything before saving, then un-encrypted data will never exist on the hard drive, but if this mechanism can arbitrarily save anything in RAM onto the hard drive, that's very dangerous!

This doesn't seem like a great reason to do this, but whatever. That unencrypted data can be read right out of RAM just as easily as reading it from the page file for somebody who is interested. There are also ways to ensure that a page file is cleared when users log out, and so on; these are all security-policy-level issues that should be in the domain of the user or the system administrator.

But if you want to make your application that user-hostile, that's your business.

10 minutes ago, myvraccount said:

Also, I'm inclined to believe that there must be some way to do it, because after all, Windows keeps track of that information, so it must be stored somewhere right?

No.

Along the same vein, Windows knows what your password is, right? Otherwise how do you log in. That doesn't mean the OS provides you a good, old-fashioned, "GiveMeTheUserPasswordInPlainText()" API you can call.

You're correct that this information is stored someplace. You might be correct that you can get at it somehow, that it's not flat-out inaccessible to user mode code. However, I know of no documented mechanism to do so, offhand, and if you're worried about security, doing this in an undocumented fashion is basically shooting yourself in the foot, so there's that.

 

This topic is closed to new replies.

Advertisement