Discussion:
takes a long time to release memory
(too old to reply)
Michaelov, Itzik
2008-05-04 07:24:17 UTC
Permalink
I'm using VB 6.0 SP6
why in second case memory release very slowly

Case 1: (execution time 2 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = 1 To lCount
mcol.Remove 1
Next lIndex
Set mcol = Nothing


Case 2: (execution time 30 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = lCount To 1 Step -1
mcol.Remove lIndex
Next lIndex
Set mcol = Nothing


Thanks
Tony Proctor
2008-05-04 09:22:29 UTC
Permalink
Because, to find a Collection entry by index, the Collection has to traverse
a linked list. In other words, the second case is doing a "serial search" to
find the last element each time, but the first case is only looking for the
very first element each time


Tony Proctor
Post by Michaelov, Itzik
I'm using VB 6.0 SP6
why in second case memory release very slowly
Case 1: (execution time 2 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = 1 To lCount
mcol.Remove 1
Next lIndex
Set mcol = Nothing
Case 2: (execution time 30 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = lCount To 1 Step -1
mcol.Remove lIndex Next lIndex
Set mcol = Nothing
Thanks
Itzik
2008-05-04 11:59:50 UTC
Permalink
Thanks,
but if you execute my samples, you will see, that behavior of memory
managment different
between two cases
Do you know why ?

Thanks
Post by Tony Proctor
Because, to find a Collection entry by index, the Collection has to
traverse a linked list. In other words, the second case is doing a "serial
search" to find the last element each time, but the first case is only
looking for the very first element each time
Tony Proctor
Post by Michaelov, Itzik
I'm using VB 6.0 SP6
why in second case memory release very slowly
Case 1: (execution time 2 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = 1 To lCount
mcol.Remove 1
Next lIndex
Set mcol = Nothing
Case 2: (execution time 30 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = lCount To 1 Step -1
mcol.Remove lIndex Next lIndex
Set mcol = Nothing
Thanks
Ralph
2008-05-04 14:20:14 UTC
Permalink
Post by Itzik
Thanks,
but if you execute my samples, you will see, that behavior of memory
managment different
between two cases
Do you know why ?
The memory management is identical.

Collections have 'two' keys.

One is an Index which refers to the order in which the objects are listed in
the linked list of objects. It is not a direct property of any one object.
It is a relative reference based on the current state of the collection.
When you remove the 'first' object the 'next' object becomes the 'first',
etc.

So just as Tony mentioned, when you run Case #1 the Collection merely grabs
the first one and removes it. ie, One 'click'.

But when you run Case #2 the Collection has to enumerate down through the
list till it finds the 'nth' one and then remove it. ie, 120000 'clicks' the
first time, 119999 'clicks' the second time, ...

I mention the second type of "Key" only to keep balance. This is a hashed
string identifier which sticks with the inserted object. The Collection can
retrieve these strings faster than it can an index.

Play around with stuff like this...

For lCount = 1 To 120000
Set k = New Collection
' mcol.Add k
' build a string key out of the 'index'
mcol.Add k, "Key" & CStr(lCount)
Next
lCount = mcol.Count
For lIndex = 1 To lCount
'mcol.Remove 1
mcol.Remove "Key" & CStr(lCount)
Next lIndex

For lCount = 1 To 120000
Set k = New Collection
mcol.Add k, "Key" & CStr(lCount)
Next
lCount = mcol.Count
For lIndex = lCount To 1 Step -1
mcol.Remove "Key" & CStr(lIndex)
Next lIndex

-ralph
Ralph
2008-05-04 15:21:11 UTC
Permalink
Post by Michaelov, Itzik
I'm using VB 6.0 SP6
why in second case memory release very slowly
Case 1: (execution time 2 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = 1 To lCount
mcol.Remove 1
Next lIndex
Set mcol = Nothing
Case 2: (execution time 30 seconds)
Dim mcol As New Collection
Dim k As Collection
For lCount = 1 To 120000
Set k = New Collection
mcol.Add k
Next
lCount = mcol.Count
For lIndex = lCount To 1 Step -1
mcol.Remove lIndex
Next lIndex
Set mcol = Nothing
Thanks
Just in case...

To merely release all the memory in a Collection you don't have to 'unload'
all the objects. Just setting "mcol = Nothing" will do the trick.

-ralph

Loading...