Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default VBA memory allocation

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default VBA memory allocation


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"
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.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

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.

" wrote:

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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

Thanks, but this isnt the reason. I am selecting the right ones to delete.

"Jim Cone" wrote:


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"
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.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default VBA memory allocation


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"
wrote in message
Thanks, but this isnt the reason. I am selecting the right ones to delete.



"Jim Cone" wrote:
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"
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.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

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.

"Jim Cone" wrote:


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"
wrote in message
Thanks, but this isnt the reason. I am selecting the right ones to delete.



"Jim Cone" wrote:
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"
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.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default VBA memory allocation

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.

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default VBA memory allocation


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"
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.

"Jim Cone" wrote:


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"
wrote in message
Thanks, but this isnt the reason. I am selecting the right ones to delete.



"Jim Cone" wrote:
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"
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.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

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.


" wrote:

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.




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default VBA memory allocation

On Jun 25, 1:44 pm, vivmaha wrote:
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


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

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.

" wrote:

On Jun 25, 1:44 pm, vivmaha wrote:
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



  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default VBA memory allocation

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.

"vivmaha" wrote:

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.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XL 2007 - Out of Memory - memory leak/bug? PCLIVE Excel Discussion (Misc queries) 0 March 23rd 09 03:31 PM
Allocation JMI Excel Discussion (Misc queries) 0 February 10th 09 05:49 PM
Allocation Dinesh Excel Worksheet Functions 4 June 28th 07 10:06 PM
XLL Memory allocation problem Nat[_4_] Excel Programming 0 March 6th 06 11:04 AM
F-key allocation HELLBOY787 Excel Discussion (Misc queries) 1 June 30th 05 11:21 AM


All times are GMT +1. The time now is 04:20 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"