Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to tell if system has virtual memory / page file on?

I have an application that consumes large amounts of RAM that I deploy to users. Some of my users are running into out of memory exception when running it - and I am noticing this is because they have their system page file turned off (because who would use 16GB of memory these days? sigh...). I want to detect if user has set this to off (or maybe some other settings) so I can warn them, because we have a lot of users come to us for support and I want to automate out some of the users because they are eating up lots of our time.

I have googled around and I can't seem to find a way to get information about page file. Specifically, I am talking about information you can see in this page in windows: Page file settings in windows

I know this is our end users problem and has nothing to do with our application (our app is designed to use up a good chunk of memory and gets a significant speed benefit). I am unsure how to detect these kinds of settings - does anyone have an idea?

like image 903
Mgamerz Avatar asked May 21 '18 02:05

Mgamerz


2 Answers

You'll need to add reference to System.Management beforehand.

AllocatedBaseSize will show the current page file size in MB

using (var query = new ManagementObjectSearcher("SELECT AllocatedBaseSize FROM Win32_PageFileUsage"))
        {
            foreach (ManagementBaseObject obj in query.Get())
            {
                uint used = (uint)obj.GetPropertyValue("AllocatedBaseSize");
                Console.WriteLine(used);
            }
        }

While MaximumSize will show the maximum page file size in MB, if the user set the maximum size (if the system managed it, the query won't return anything).

using (var query = new ManagementObjectSearcher("SELECT MaximumSize FROM Win32_PageFileSetting"))
        {
            foreach (ManagementBaseObject obj in query.Get())
            {
                uint max = (uint)obj.GetPropertyValue("MaximumSize");
                Console.WriteLine(max);
            }
        }

If the AllocatedBaseSize is less than what your app will use and the MaximumSize is large enough for your app (or it's system managed), you'll need to consider the edge case where the storage is not enough for Windows to grow the page file. Even if there is enough space in the beginning, user could be downloading a large file on other program or rendering a large video while running your app. Consider offering 'low storage' mode where your app may run slower but don't consume as much memory.

like image 114
Martheen Avatar answered Sep 22 '22 08:09

Martheen


Whilst I don't have a complete working solution for you, I think the information you are after can be retrieved from the Win32_PageFileUsage WMI class. The AllocatedBaseSize property should contain the information you are after:

AllocatedBaseSize

Data type: uint32

Access type: Read-only

Qualifiers: MappingStrings ("Win32API|MEMORYSTATUS|dwTotalPageFile"), units ("megabytes")

Actual amount of disk space allocated for use with this page file. This value corresponds to the range established in Win32_PageFileSetting under the InitialSize and MaximumSize properties, set at system startup. Example: 178

like image 33
Adrian Sanguineti Avatar answered Sep 20 '22 08:09

Adrian Sanguineti