I’m writing a multiple undo/redo system for a custom textbox control in Silverlight. The thing I’m working on now is reducing memory consumption.
So the problem I’m having is that the stacks I’m using are being held in memory too long.
I figure it’d be relatively inexpensive to erase the stack altogether whenever its Count reaches 0 naturally, or when the stack is cleared due to an event. So I’m attempting to do that with this code, hoping that’ll be picked up by the GC…
TextHistory.Clear()
TextHistory = Nothing
But that’s definitely not working, and this stack can potentially hold 50 MB or more. By the way, TextHistory is a Stack(Of Moment). And here’s the Moment class…
Public Class Moment
Public Text As String
Public SelectionStart As Integer
Public SelectionLength As Integer
Public Sub New(ByRef _Text As String, _SelectionStart As Integer, _SelectionLength As Integer)
Text = _Text
SelectionStart = _SelectionStart
SelectionLength = _SelectionLength
End Sub
End Class
In the comments you said that forcing a GC works (works = cleans up the garbage). That is how it is supposed to work.
GC happens on demand. Demand is either lots of allocations happened, memory pressure or manual trigger. None of these happened which is why the garbage did not get cleaned up.
GC does not run time-based (say every minute or so).