C # Windows CE.net 3.5 to check memory usage

I am new to this place and starting with C # mobile. Now I work on the C # handheld device platform. So, I have a question to learn how to get memory usage. I tried GC.GetTotalMemory () and got the allocated memory that GC used. But can I use this to estimate how much my application has been allocated for memory. I suppose it could be, but not quite right. Then I tried google to search for any link or class or something that could be used to check memory in Windows CE, but I only found on another platform that is not supported by what I am doing.

Thanks in advance, Stoper


I apologize for leaving this post.

I really thank everyone who answered my question and looked at this post.

Now I have everything I need by implementing the "OpenNetCF" link in my project.

Thanks again;)

+5
source share
2 answers

According to the documents GC.GetTotalMemoryreturns

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

/ , , . , , , (.. ) .

. , GDI ( , ..), . , , .

, , , , ( GC. GetTotalMemory ). P/Invoking GlobalMemoryStatus , . MSDN .

+6

. , , , , 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");
    /*************************************************************************/
}
+3

All Articles