C# Windows CE .net 3.5 检查内存使用情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6935995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:41:02  来源:igfitidea点击:

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

c#windows.net-3.5windows-cehandheld

提问by Sathapanic Sriprom

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.

我是这个地方的新手,也是 C# 移动版的初学者。现在,我正在研究 C# 手持设备平台。所以,我有一些问题要问如何获取内存使用情况。我尝试了GC.GetTotalMemory()并获取了 GC 使用的分配内存。但是,我可以用它来估计我的应用程序分配了多少内存。我想这可能是,但实际上并不正确。然后我尝试谷歌搜索任何参考或类或任何用于检查 Windows CE 上的内存的东西,但我只在另一个平台中找到了我正在做的事情不支持的平台。

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 project.

现在,通过在我的项目中实现“OpenNetCF”参考,我得到了我需要的一切。

Thanks again ;)

再次感谢 ;)

回答by ctacke

According to the docs, GC.GetTotalMemoryreturns

根据文档,GC.GetTotalMemory返回

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 internallybut not what its actual allocation for the whole heap (i.e. allocated and unallocatedmanaged memory) from the system is.

这对某些开发人员来说有点误导/混淆,尤其是那些来自本地世界的开发人员。它会告诉您 GC 在内部分配了多少内存,但不会告诉您它从系统中为整个堆(即已分配和未分配的托管内存)实际分配的内存是多少。

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.

它也不报告本机分配。如果您使用大量 GDI 对象(位图、画笔等),这可能会很大,因为它们也具有本机内存分配。在位图的情况下,它的管理足迹实际上比它的本机足迹小得多。

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 virtualmemory it has to get an actual feel for what's going on (I find GC.GetTotalMemory to be relatively useless in fact). P/Invoking GlobalMemoryStatusgives you what you want. MSDN contains an example.

如果您对托管应用程序对整个系统资源的实际影响感兴趣,您需要查询操作系统并询问它需要多少物理和虚拟内存才能真正了解正在发生的事情(我发现 GC.GetTotalMemory 是实际上比较没用)。P/Invoking GlobalMemoryStatus给你你想要的。MSDN 包含一个示例

回答by CCS

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...

尝试这个。它会给你我认为你想要的东西,包括你用 GC.GetTotalMemory() 得到的东西。您必须将文本标签替换为您自己的标签或以任何您想要的方式使用它。不过,您将不得不使用 P/Invoke...

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");
    /*************************************************************************/
}