VBA memory allocation

V

vivmaha

Hi,

I'm running a huge model on VBA, and I think i'm leaking memory.

In the following example, does the memory allocated for myInstance get freed
when I remove it from the collection? I assume that, like java, once the
memory is not referenced to, It gets deallocated.

dim myCollection as Collection
Set myCollection = new Collection
dim myInstance as MyClass
Set myInstance = new MyClass
myCollection.add myInstance
myCollection.remove 1

If the above does not free the memory, how do I do it?

Also, does VBA have constructors? And destructors?

Thanks.
 
G

gimme_this_gimme_that

In Java the object doesn't get removed from memory until the Java VM
runs a garbage collector.

It's possible to evoke the garbage collector from Java, but
utilimately the VM has control over the object.

Try this (although I doubt this will help) :

set myCollection=Nothing

The thing about collections is that you often end up using them
globally.

Probably what you need to do is iterate over your collection, set each
member to Nothing, then set the collection to Nothing.
 
J

Jim Cone

One possible cause...
Adding a member to a collection without specifying an index causes
the member to be appended to the end of the collection.
So to remove the most recent addition you would use...
myCollection.Remove myCollection.Count

If you want to remove the entire collection from memory then...
Set myCollection = Nothing
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"vivmaha" <[email protected]>
wrote in message
Hi,

I'm running a huge model on VBA, and I think i'm leaking memory.

In the following example, does the memory allocated for myInstance get freed
when I remove it from the collection? I assume that, like java, once the
memory is not referenced to, It gets deallocated.

dim myCollection as Collection
Set myCollection = new Collection
dim myInstance as MyClass
Set myInstance = new MyClass
myCollection.add myInstance
myCollection.remove 1

If the above does not free the memory, how do I do it?

Also, does VBA have constructors? And destructors?

Thanks.
 
V

vivmaha

I tried this.
I can't set every one in my collection to nothing, as I'm using some.

So I tried to set selected items from the collection to nothing, and then
remove them:

i=1 'Collection
iteration counter
While i<collect.count 'while i have more
items to check
if collect.item(i).isTimeToRemove then 'if i'm spose to remove this
item
Set collect.item(i) = nothing 'set it to null
collect.remove(i) 'take it out of the
collection
else 'if i'm not
spose to remove this item
i=i+1 'go on to check
the next item
endif
wend

The "Set collect.item(i) = nothing" doesnt get executed for some reason...

Thanks,
Vivek.
 
J

Jim Cone

A Collection is 1 based not 0 based.
Also a Collection is re-indexed after an item is removed.
It is best to work from the end to the beginning...
For N = myCollection.Count to 1 Step -1
If TimeToRemove... then
Set myCollection.Item(N) = Nothing
myCollection.Remove N
End If
Next
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




"vivmaha" <[email protected]>
wrote in message
Thanks, but this isnt the reason. I am selecting the right ones to delete.
 
V

vivmaha

I knew a collection is 1 based. Did I say something to imply otherwise?

When u say a collection is reindexed, is this done in any predictable manner?

Also, why is your (more elegant) reverse way of traversing the collection
better than my way?

Thanks.
 
G

gimme_this_gimme_that

You may be assigning the reference to your object to Nothing but not
actually removing the object from memory. What do you think of this?

i=1
iteration counter
While i<collect.count
set myobject = collect.item(i)
if myobject.isTimeToRemove then
set myobject = Nothing
set collect.item(i) = nothing
collect.remove(i)
else
sose to remove this item
i=i+1
endif
wend

Also consider changing the line
set myobject = Nothing
to a custom sub that sets the members of myobject to nothing.
 
J

Jim Cone

Oops, I was reading your code as increasing the counter on each loop.
I was wrong, your code does work, with the exception of it exiting before the
last item. (which I assume is intentional)

A collection leaves no blanks when a removal is done.
It simply re-indexes those items above the removed item.
So if you remove item 1 of ten items , then the remaining items are indexed
as 1 thru 9 ( they were 2 thru 10)
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"vivmaha" <[email protected]>
wrote in message
I knew a collection is 1 based. Did I say something to imply otherwise?

When u say a collection is reindexed, is this done in any predictable manner?

Also, why is your (more elegant) reverse way of traversing the collection
better than my way?

Thanks.
 
V

vivmaha

I think you are right. I wrongly assumed that VBA was like JAVA and had a
'garbage collector' etc.

However, I dont know how to fix the problem. I'm getting an error when the
"collect.item(1)=nothing" line is called.

Here is the code:

Public Sub run()

Dim C As Collection
Set C = New Collection

Dim mc As Class1
Set mc = New Class1

C.Add mc

Set C.Item(1) = Nothing '<- Error thrown here

End Sub

The error is:
Run-time error '438':
Object doesn't support this property or method


Help.
 
G

gimme_this_gimme_that

I think you are right. I wrongly assumed that VBA was like JAVA and had a
'garbage collector' etc.

However, I dont know how to fix the problem. I'm getting an error when the
"collect.item(1)=nothing" line is called.

Here is the code:

Public Sub run()

Dim C As Collection
Set C = New Collection

Dim mc As Class1
Set mc = New Class1

C.Add mc

''''' Not required Set C.Item(1) = Nothing '<- Error thrown
here
mc = Nothing ' This is all you need
set C = Nothing

End Sub
 
V

vivmaha

That example was just to show the error i was getting. I dont actually have
references to items in the collection, such as mc. I returned the ref. and
set it to zero, but i wanted to know why the coll.item(1) = nothing doesnt
work.

Thanks.
 
V

vivmaha

Ok guys.

I did something that I should have done hours ago: i made some simple
programs to test the memory mechanics in VBA.

It turns out that coll.remove(index) also frees memory of the object at
'index' PROVIDED THAT nothing else refers to the object. In fact coll=nothing
also does this. My memory isnt getting freed as something else is pointing to
the object i'm try to free. And i'll be spending the rest of this nice day
look for that.

Thanks for all ur help.
Vivek.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top