简体   繁体   English

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

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

I'm a newbie in this place and starter with C# mobile. 我是这个地方的新手,也是C#mobile的首发。 Now , I'm working on C# handheld device platform. 现在,我正在研究C#手持设备平台。 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. 我尝试了GC.GetTotalMemory()并获取GC使用的已分配内存。 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. 然后我尝试谷歌搜索任何参考或类或任何用于检查Windows CE内存的东西,但我发现只在另一个平台不支持我正在做的事情。

Thanks in advance , Stoper 先谢谢,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 ;) 再次感谢 ;)

According to the docs, GC.GetTotalMemory returns 根据文档, 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 internally but not what its actual allocation for the whole heap (ie allocated and unallocated managed 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. 如果您使用大量GDI对象(位图,画笔等),那么这可能是巨大的,因为它们也具有本机内存分配。 In teh case of a Bitmap, it's managed footprint is actually much smaller than its native footprint. 在Bitmap的情况下,它的托管占用空间实际上比其原生占用空间小得多。

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). 如果您对托管应用程序对整个系统资源的实际影响感兴趣,您需要查询操作系统并询问它有多少物理和虚拟内存才能真正感受到正在发生的事情(我发现GC.GetTotalMemory是事实上相对无用)。 P/Invoking GlobalMemoryStatus gives you what you want. P /调用GlobalMemoryStatus为您提供所需的内容。 MSDN contains an example . MSDN包含一个示例

Try this. 试试这个。 It will give you what I think you want, including what you had with GC.GetTotalMemory(). 它会给你我想你想要的东西,包括你对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... 你将不得不使用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");
    /*************************************************************************/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM