Archive

Posts Tagged ‘Memory’

Memory Stats on Mobile Device

December 10th, 2009 robber.baron No comments

Recently I was working on a mobile project that was experiencing some serious memory leak issues. It turns out that I wasn’t disposing for a SqlCeResultSet properly and on each query (of which there was substantial amount) I was calling New() without Dispose().

After a day of feeling like my project was falling apart before my eyes and getting weird and unreliable results from the .NETCF Remote Profiler I searched far a wide for some way to determine the available memory on a device. On MSDN I found the following code (slightly modified by me) to get relevant information.

Basically two P/Invokes later I had the information I needed, saw that I was in fact leaking memory, quickly determined where, and restored sanity to my project. Within thirty minutes of implementing this class the world was a happier place for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Imports System.Text
 
Public Class MEMORYSTATUSINFO
    Public Structure MEMORYSTATUS
        Public dwLength As UInt32
        Public dwMemoryLoad As UInt32
        Public dwTotalPhys As UInt32
        Public dwAvailPhys As UInt32
        Public dwTotalPageFile As UInt32
        Public dwAvailPageFile As UInt32
        Public dwTotalVirtual As UInt32
        Public dwAvailVirtual As UInt32
    End Structure
 
    Public Declare Function GlobalMemoryStatus Lib "CoreDll.Dll" _
    (ByRef ms As MEMORYSTATUS) As Integer
 
    Public Declare Function GetSystemMemoryDivision Lib "CoreDll.Dll" _
        (ByRef lpdwStorePages As UInt32, _
        ByRef ldpwRamPages As UInt32, _
        ByRef ldpwPageSize As UInt32) As Integer
 
    Public Shared Function ShowMemory() As String
        Dim storePages As UInt32
        Dim ramPages As UInt32
        Dim pageSize As UInt32
        Dim res As Integer = _
            GetSystemMemoryDivision(storePages, ramPages, pageSize)
 
        ' Call the native GlobalMemoryStatus method
        ' with the defined structure.
        Dim memStatus As New MEMORYSTATUS
        GlobalMemoryStatus(memStatus)
 
        Dim load As Integer = memStatus.dwMemoryLoad / (1024 * 1024)
        Dim totPhys As Integer = memStatus.dwTotalPhys / (1024 * 1024)
        Dim availPhys As Integer = memStatus.dwAvailPhys / (1024 * 1024)
        Dim totalPageFile As Integer = memStatus.dwTotalPageFile
        Dim availPageFile As Integer = memStatus.dwAvailPageFile
        Dim totVirtual As Integer = memStatus.dwTotalVirtual / (1024 * 1024)
        Dim availVirtual As Integer = memStatus.dwAvailVirtual / (1024 * 1024)
 
        ' Use a StringBuilder for the message string.
        Dim MemoryInfo As New StringBuilder
        MemoryInfo.Append("Memory Load: " _
            & load.ToString() & "Mb" & vbCrLf)
        MemoryInfo.Append("Total Physical: " _
            & totPhys & "Mb" & vbCrLf)
        MemoryInfo.Append("Avail Physical: " _
            & availPhys & "Mb" & vbCrLf)
        MemoryInfo.Append("Total Page File: " _
            & totalPageFile.ToString() & vbCrLf)
        MemoryInfo.Append("Avail Page File: " _
            & availPageFile.ToString() & vbCrLf)
        MemoryInfo.Append("Total Virtual: " _
            & totVirtual.ToString() & "Mb" & vbCrLf)
        MemoryInfo.Append("Avail Virtual: " _
            & availVirtual.ToString() & "Mb" & vbCrLf)
 
        ' Show the available memory.
        Return MemoryInfo.ToString()
    End Function
End Class

Hope this helps someone else find greedy memory hog code in their apps.