![]() |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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 |
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 |
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. |
All times are GMT +1. The time now is 05:10 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com