2009年10月13日火曜日

.NET(Mobile) メモリ使用量を表示する

ComactFrameworkでモバイルのメモリ使用量を表示するサンプルです。

Public Class Memory
    Public Structure MEMORYSTATUS
         Public dwLength As Integer
         Public dwMemoryLoad As Integer
         Public dwTotalPhys As Integer         '合計 物理メモリ
         Public dwAvailPhys As Integer         '空き 物理メモリ
         Public dwTotalPageFile As Integer
         Public dwAvailPageFile As Integer
         Public dwTotalVirtual As Integer      '合計 仮想メモリ
         Public dwAvailVirtual As Integer      '空き 仮想メモリ
    End Structure

     _
    Public Shared Function GlobalMemoryStatus(ByRef ms As MEMORYSTATUS) As Integer
    End Function

     _
    Public Shared Function GetSystemMemoryDivision(ByRef lpdwStorePages As Integer, ByRef ldpwRamPages As Integer, ByRef ldpwPageSize As Integer) As Integer
    End Function


    Public Shared Sub ShowMemory()
        Dim storePages As Integer
        Dim ramPages As Integer
        Dim pageSize As Integer
        Dim res As Integer = GetSystemMemoryDivision(storePages, ramPages, pageSize)

        Dim memStatus As New MEMORYSTATUS
        GlobalMemoryStatus(memStatus)

        Dim MemoryInfo As New System.Text.StringBuilder
        MemoryInfo.AppendLine("合計物理メモリ: " & memStatus.dwTotalPhys.ToString("#,##0"))
        MemoryInfo.AppendLine("使用物理メモリ: " & (memStatus.dwTotalPhys - memStatus.dwAvailPhys).ToString("#,##0"))
        MemoryInfo.AppendLine("空き物理メモリ: " & memStatus.dwAvailPhys.ToString("#,##0"))
        MemoryInfo.AppendLine("使用率:" & CInt(((memStatus.dwTotalPhys - memStatus.dwAvailPhys) / memStatus.dwTotalPhys) * 100) & "%")
        MemoryInfo.AppendLine()
        MemoryInfo.AppendLine("合計仮想メモリ: " & memStatus.dwTotalVirtual.ToString("#,##0"))
        MemoryInfo.AppendLine("使用仮想メモリ: " & (memStatus.dwTotalVirtual - memStatus.dwAvailVirtual).ToString("#,##0"))
        MemoryInfo.AppendLine("空き仮想メモリ: " & memStatus.dwAvailVirtual.ToString("#,##0"))
        MemoryInfo.AppendLine("使用率:" & CInt(((memStatus.dwTotalVirtual - memStatus.dwAvailVirtual) / memStatus.dwTotalVirtual) * 100) & "%")
        MemoryInfo.AppendLine()
        MemoryInfo.AppendLine("GC.GetTotalMemory:" & GC.GetTotalMemory(False).ToString("#,##0"))
        System.Windows.Forms.MessageBox.Show(MemoryInfo.ToString())
    End Sub
End Class



Memory.ShowMemory()

0 件のコメント: