开发者

C# Windows CE .net 3.5 to checked the memory usage

开发者 https://www.devze.com 2023-03-24 22:33 出处:网络
I\'m a newbie in this place and starter with C# mobile. Now , I\'m working on C# handheld device platform. So , I have some question to ask about how to get the memory usage.

I'm a newbie in this place and starter with C# mobile. Now , I'm working on C# handheld device platform. So , I have some question to ask about how to get the memory usage. I have try GC.GetTotalMemory() and get the allocated memory that the GC used. But , Can I use this to estimated that how much my application was allocated the memory. I suppose that it may be but not actual correct. Then I've try google to searching for any reference or class or anything to use for checked the memory on windows CE but I've found only in another platform that not supported with the thing I'm doing.

Thanks in advance , Stoper


Apologize for that I gone away from this post.

I'm really thank you to anybody who've answer my question and watch on this post.

Now , I got all that I need by implement the "OpenNetCF" reference in my p开发者_如何转开发roject.

Thanks again ;)


According to the docs, GC.GetTotalMemory returns

A number that is the best available approximation of the number of bytes currently allocated in managed memory.

This is a bit misleading/confusing to some devs, especially those coming from a native world. It's going to tell you how much memory the GC has allocated internally but not what its actual allocation for the whole heap (i.e. allocated and unallocated managed memory) from the system is.

It also doesn't report native allocations. This can be huge if you use a lot of GDI objects (bitmaps, brushes, etc) as those have native memory allocations as well. In teh case of a Bitmap, it's managed footprint is actually much smaller than its native footprint.

If you're interested in your managed apps actual impact on the overall system resources, you need to query the OS and ask how much physical and virtual memory it has to get an actual feel for what's going on (I find GC.GetTotalMemory to be relatively useless in fact). P/Invoking GlobalMemoryStatus gives you what you want. MSDN contains an example.


Try this. It will give you what I think you want, including what you had with GC.GetTotalMemory(). You would have to replace the text lables with your own or use it any way you want. You will have to use P/Invoke though...

public struct MEMORYSTATUS
{
    public UInt32 dwLength;
    public UInt32 dwMemoryLoad;
    public UInt32 dwTotalPhys;
    public UInt32 dwAvailPhys;
    public UInt32 dwTotalPageFile;
    public UInt32 dwAvailPageFile;
    public UInt32 dwTotalVirtual;
    public UInt32 dwAvailVirtual;
}

[DllImport("coredll.dll")]
private static extern void GlobalMemoryStatus(out MEMORYSTATUS lpBuffer);

public static void GetGlobalMemoryStatus(out UInt32 dwTotal, out UInt32 dwAvail,
                                             out UInt32 dwProcTotal, out UInt32 dwProcAvail)
{
    MEMORYSTATUS status = new MEMORYSTATUS();
    output.Length = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(output);
    GlobalMemoryStatus(out status);

    dwTotal = status.dwTotalPhys;
    dwAvail = status.dwAvailPhys;
    dwProcTotal = status.dwTotalVirtual;
    dwProcAvail = status.dwAvailVirtual;
}

private void UpdateMemoryDisplay()
{
    /*************************************************************************/
    bool IsTotalGB = false;
    bool IsUsedGB = false;
    uint TotalRamMemory;
    uint AvailRamMemory;
    uint ProcTotalRamMemory;
    uint ProcAvailRamMemory;

    GetGlobalMemoryStatus(out TotalRamMemory, out AvailRamMemory,
                          out ProcTotalRamMemory, out ProcAvailRamMemory);

    float TotalMB = (float)((float)TotalRamMemory / (1024 * 1024));
    float UsedMB = TotalMB - (float)((float)AvailRamMemory / (1024 * 1024));

    int RamPercent = (int)((UsedMB / TotalMB) * 100.0);

    if (1000 < TotalMB)
    {
        TotalMB /= 1000;
        IsTotalGB = true;
    }

    if (1000 < UsedMB)
    {
        UsedMB /= 1000;
        IsUsedGB = true;
    }

    this.RamMemMinLbl.Text = UsedMB.ToString("0.0") + ((false != IsUsedGB) ? "GB" : "MB");
    this.RamMemMaxLbl.Text = TotalMB.ToString("0.0") + ((false != IsTotalGB) ? "GB" : "MB");
    this.RamMemPercent.Current = RamPercent;

    IsUsedGB = false;

    TotalMB = (float)((float)ProcTotalRamMemory / (1024 * 1024));
    UsedMB = TotalMB - (float)((float)ProcAvailRamMemory / (1024 * 1024));

    if (1000 < UsedMB)
    {
        UsedMB /= 1000;
        IsUsedGB = true;
    }

    this.ProcRamMemMinLbl.Text = UsedMB.ToString("0.0") + ((false != IsUsedGB) ? "GB" : "MB");

    IsUsedGB = false;

    UsedMB = (float)((float)GC.GetTotalMemory(false) / (1024 * 1024));

    if (1000 < UsedMB)
    {
        UsedMB /= 1000;
        IsUsedGB = true;
    }

    this.GCMemMinLbl.Text = UsedMB.ToString("0.0") + ((false != IsUsedGB) ? "GB" : "MB");
    /*************************************************************************/
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号