Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6529463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:38:18+00:00 2026-05-25T09:38:18+00:00

how to dispay total memory usage in percentage most work on windows XP and

  • 0

how to dispay total memory usage in percentage most work on windows XP and 7 (.net2)

I have tried the following solutions without succes (crashes or freezes)

http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html

and using

System.GC.GetTotalMemory(true);

working sample thank to Darin,

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public class MEMORYSTATUSEX
    {
        public uint dwLength = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MEMORYSTATUSEX));
        public uint dwMemoryLoad;
        public ulong ullTotalPhys;
        public ulong ullAvailPhys;
        public ulong ullTotalPageFile;
        public ulong ullAvailPageFile;
        public ulong ullTotalVirtual;
        public ulong ullAvailVirtual;
        public ulong ullAvailExtendedVirtual;
    }
    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
    static extern bool GlobalMemoryStatusEx(MEMORYSTATUSEX lpBuffer);

    private void timer1_Tick(object sender, EventArgs e)
    {
        var result = new MEMORYSTATUSEX();
        if (GlobalMemoryStatusEx(result))
            getpercentage(((double)result.ullTotalPhys - result.ullAvailPhys) / result.ullTotalPhys);
    }

    static void getpercentage(double ratio)
    {
        string usage = string.Format("usage {0:0%}", ratio);
        Console.WriteLine(usage);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:38:19+00:00Added an answer on May 25, 2026 at 9:38 am

    If you want to use the GlobalMemoryStatusEx method, here’s an example:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class MEMORYSTATUSEX
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public ulong ullTotalPhys;
        public ulong ullAvailPhys;
        public ulong ullTotalPageFile;
        public ulong ullAvailPageFile;
        public ulong ullTotalVirtual;
        public ulong ullAvailVirtual;
        public ulong ullAvailExtendedVirtual;
        public MEMORYSTATUSEX()
        {
            this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
        }
    }
    
    class Program
    {
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
    
        static void Main()
        {
            MEMORYSTATUSEX result = new MEMORYSTATUSEX();
            if (GlobalMemoryStatusEx(result))
            {
                Console.WriteLine(
                    "{0}% memory left", 
                    100.0 * result.ullAvailPhys / result.ullTotalPhys
                );
            }
        }
    }
    

    Another possibility if you don’t like Interop is to use the ComputerInfo class (you just need to add reference to the Microsoft.VisualBasic assembly):

    class Program
    {
        static void Main()
        {
            Microsoft.VisualBasic.Devices.ComputerInfo ci = new Microsoft.VisualBasic.Devices.ComputerInfo();
            Console.WriteLine(
                "{0}% memory left",
                100.0 * ci.AvailablePhysicalMemory / ci.TotalPhysicalMemory
            );
        }
    }
    

    And yet another possibility is to query the performance counters to find out the available memory:

    class Program
    {
        static void Main()
        {
            // You could also use "Available Bytes", "Available KBytes", "Available MBytes"
            PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes");
            Console.WriteLine(Convert.ToInt64(pc.NextValue()));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following dataset NAME | GP | ORD_GP | EXP | TOTAL
I want to display total number of user who only have posts at Wordpress.
So I have this extremely memory-intensive Java application I'm building, which builds a tree
I'm trying to track down a memory leak in a C++ application in Windows
I have following Crystal Report: Here data which is displayed have period 200804 to
I have a gridview with a footer and I display the total of the
I have to get a running total from four textboxes and have that total
Ok I need to display total talk time of an agent that is coming
I want to display the total number of comments for a particular post in
I need to pad numbers with leading zeros (total 8 digits) for display. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.