i wish it was more known to the outside world how .Net handles memory
for one of our customers i had to built this code in one of our distributed
apps :-)
Public Sub New()
MyBase.New()
M_dtLastUsage = Date.Now
If IsNothing(oTimer) Then
oTimer = New System.Threading.Timer(oCallback, Nothing,
System.TimeSpan.FromMinutes(0), System.TimeSpan.FromMinutes(1))
End If
End Sub
Private M_dtLastUsage As DateTime
Private oCallback As New TimerCallback(AddressOf OnTick)
Private oTimer As Threading.Timer
Private Declare Auto Function SetProcessWorkingSetSize Lib "kernel32.dll"
(ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As
Boolean
Public Sub OnTick(ByVal stateInfo As Object)
Dim DtCurrent As DateTime = Date.Now
Dim elapsed_time As TimeSpan
elapsed_time = DtCurrent.Subtract(M_dtLastUsage)
If elapsed_time.TotalMinutes >= 1 Then
oTimer.Dispose() : oTimer = Nothing
Try
Dim Mem As Process = Process.GetCurrentProcess()
SetProcessWorkingSetSize(Mem.Handle, -1, -1)
Catch ex As Exception
End Try
End If
End Sub
every time the user performs an action this method is called in a singleton
class
Friend Function fblnGuidExists(ByVal strGuid As String, ByRef stUservals As
SessionVals) As Boolean
M_dtLastUsage = Date.Now
If IsNothing(oTimer) Then
oTimer = New System.Threading.Timer(oCallback, Nothing,
System.TimeSpan.FromMinutes(0), System.TimeSpan.FromMinutes(1))
End If
If htSessions.ContainsKey(strGuid) Then
Dim structSessionVals As SessionVals = CType(htSessions.Item(strGuid),
SessionVals)
With structSessionVals
..dtLastSessionAccess = Date.Now
End With
htSessions.Remove(strGuid)
htSessions.Add(strGuid, structSessionVals)
stUservals = structSessionVals
Return True
End If
End Function
I guess i do not have to tell you what happens here ;-)
by the way the customer is verry happy now
regards
Michel Posseth [MCP]
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
"Teemu" <ts*****@hotmail.com> schrieb:I have very simple app that has some buttons and labels. When I start my
app, Task manager shows that application is using about 20 mb memory,
quite a lot in my opinion.
Don't worry about that too much. Memory usage shown for .NET-based
applications is not accurate in task manager because it doesn't show how
much of the memory assigned to the process is really used.
Then, I minimize my app. When the app is minimized, it requires only 700
kb memory. But then the most intresting part: When I normalize my app
again from the taskbar, the app requires only 2 mb memory and in the app
nothing is happened meanwhile.
Why application requires in the first time 20 mb memory and after
minimize-maximize operation the amount is much smaller?
I suggest to take a look at this article:
The working set of an application is trimmed when its top-level window is
minimized
<URL:http://support.microsoft.com/?scid=kb;EN-US;293215>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>