<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Terminating Dependent Classes</title>
	<atom:link href="http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/</link>
	<description>Daily posts of Excel tips…and other stuff</description>
	<lastBuildDate>Thu, 09 Feb 2012 23:42:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Tushar Mehta</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29683</link>
		<dc:creator>Tushar Mehta</dc:creator>
		<pubDate>Tue, 01 Jan 2008 06:40:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29683</guid>
		<description>&lt;p&gt;Dick: Yes, you are absolutely correct.  Implementing a parent property requires some additional work to release the memory.  I went back and looked at some data structures with two-way object references that I&#039;d implemented over the past few years.  While that code still looks good, I will have to remember the issue raised by this discussion.&lt;/p&gt;
&lt;p&gt;Rob&#039;s post about the hack should prove valuable for those implementing a two-way object reference.  I assume it would work in the example I shared in my first post in this discussion.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Dick: Yes, you are absolutely correct.  Implementing a parent property requires some additional work to release the memory.  I went back and looked at some data structures with two-way object references that I&#8217;d implemented over the past few years.  While that code still looks good, I will have to remember the issue raised by this discussion.</p>
<p>Rob&#8217;s post about the hack should prove valuable for those implementing a two-way object reference.  I assume it would work in the example I shared in my first post in this discussion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Peltier</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29675</link>
		<dc:creator>Jon Peltier</dc:creator>
		<pubDate>Mon, 31 Dec 2007 16:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29675</guid>
		<description>&lt;p&gt;It might be brilliant, I can&#039;t tell. My brain hurts trying to follow this thread.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>It might be brilliant, I can&#8217;t tell. My brain hurts trying to follow this thread.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dick Kusleika</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29672</link>
		<dc:creator>Dick Kusleika</dc:creator>
		<pubDate>Mon, 31 Dec 2007 14:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29672</guid>
		<description>&lt;p&gt;Freakin&#039; brilliant Rob.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Freakin&#8217; brilliant Rob.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Bruce</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29661</link>
		<dc:creator>Rob Bruce</dc:creator>
		<pubDate>Sun, 30 Dec 2007 18:55:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29661</guid>
		<description>&lt;p&gt;Your Parent properties are causing circular references. These don&#039;t get cleared until the original process is reset. In effect, that&#039;s a memory leak.&lt;/p&gt;
&lt;p&gt;The VB workaround is an absolutely beautiful hack first demonstrated, I believe, by Bruce McKinney. First, change your internal variable for the parent object to a long. This is going to point to the object rather than refer to it. Then use the undocumented objptr and the hack objfromptr functions to convert the object to and from its pointer. This way you&#039;re never storing an object reference, so the circular reference in not created.&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container text default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;text codecolorer&quot;&gt;&#039; In your child class&lt;br&gt;
Private m_lngParentPtr As Long&lt;br&gt;
Private Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; _&lt;br&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;(dest As Any, Source As Any, ByVal bytes As Long)&lt;br&gt;
&lt;br&gt;
&#039; The Parent property&lt;br&gt;
Public Property Get Parent() As Class1&lt;br&gt;
&#160; &#160; Set Parent = ObjFromPtr(m_lngParentPtr)&lt;br&gt;
End Property&lt;br&gt;
Public Property Set Parent(obj As Class1)&lt;br&gt;
&#160; &#160; m_lngParentPtr = ObjPtr(obj)&lt;br&gt;
End Property&lt;br&gt;
&lt;br&gt;
&#039;Returns an object given its pointer.&lt;br&gt;
&#039;This function reverses the effect of the ObjPtr function.&lt;br&gt;
Private Function ObjFromPtr(ByVal pObj As Long) As Object&lt;br&gt;
&#160; &#160; Dim obj &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; As Object&lt;br&gt;
&#160; &#160; &#039; force the value of the pointer into the temporary object variable&lt;br&gt;
&#160; &#160; CopyMemory obj, pObj, 4&lt;br&gt;
&#160; &#160; &#039; assign to the result (this increments the ref counter)&lt;br&gt;
&#160; &#160; Set ObjFromPtr = obj&lt;br&gt;
&#160; &#160; &#039; manually destroy the temporary object variable&lt;br&gt;
&#160; &#160; &#039; (if you omit this step you&#039;ll get a GPF!)&lt;br&gt;
&#160; &#160; CopyMemory obj, 0&amp;, 4&lt;br&gt;
End Function&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;HTH&lt;/p&gt;
&lt;p&gt;Rob&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Your Parent properties are causing circular references. These don&#8217;t get cleared until the original process is reset. In effect, that&#8217;s a memory leak.</p>
<p>The VB workaround is an absolutely beautiful hack first demonstrated, I believe, by Bruce McKinney. First, change your internal variable for the parent object to a long. This is going to point to the object rather than refer to it. Then use the undocumented objptr and the hack objfromptr functions to convert the object to and from its pointer. This way you&#8217;re never storing an object reference, so the circular reference in not created.</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container text default">
<div style="white-space: nowrap;" class="text codecolorer">&#8216; In your child class<br />
Private m_lngParentPtr As Long<br />
Private Declare Sub CopyMemory Lib &#8220;kernel32&#8243; Alias &#8220;RtlMoveMemory&#8221; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(dest As Any, Source As Any, ByVal bytes As Long)</p>
<p>&#8216; The Parent property<br />
Public Property Get Parent() As Class1<br />
&nbsp; &nbsp; Set Parent = ObjFromPtr(m_lngParentPtr)<br />
End Property<br />
Public Property Set Parent(obj As Class1)<br />
&nbsp; &nbsp; m_lngParentPtr = ObjPtr(obj)<br />
End Property</p>
<p>&#8216;Returns an object given its pointer.<br />
&#8216;This function reverses the effect of the ObjPtr function.<br />
Private Function ObjFromPtr(ByVal pObj As Long) As Object<br />
&nbsp; &nbsp; Dim obj &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; As Object<br />
&nbsp; &nbsp; &#8216; force the value of the pointer into the temporary object variable<br />
&nbsp; &nbsp; CopyMemory obj, pObj, 4<br />
&nbsp; &nbsp; &#8216; assign to the result (this increments the ref counter)<br />
&nbsp; &nbsp; Set ObjFromPtr = obj<br />
&nbsp; &nbsp; &#8216; manually destroy the temporary object variable<br />
&nbsp; &nbsp; &#8216; (if you omit this step you&#8217;ll get a GPF!)<br />
&nbsp; &nbsp; CopyMemory obj, 0&amp;amp;, 4<br />
End Function</div>
</div>
<p>HTH</p>
<p>Rob</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fzz</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29654</link>
		<dc:creator>fzz</dc:creator>
		<pubDate>Sat, 29 Dec 2007 23:15:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29654</guid>
		<description>&lt;p&gt;Unless your Terminate methods are referring to global public original instances of the parent objects, I suspect your memory leak is akin to the difference between the two numbers in the last line of output from&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; test()&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; c1 &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;, c2 &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; c2 = c1&lt;br&gt;
&#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; countof(c1), countof(c2)&lt;br&gt;
&#160; c1.Add 1, &lt;span class=&quot;st0&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;br&gt;
&#160; c1.Add 2, &lt;span class=&quot;st0&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;br&gt;
&#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; countof(c1), countof(c2)&lt;br&gt;
&#160; c2.Add 3, &lt;span class=&quot;st0&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;br&gt;
&#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; countof(c1), countof(c2)&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; c2 = &lt;span class=&quot;kw1&quot;&gt;Nothing&lt;/span&gt;&lt;br&gt;
&#160; Debug.&lt;span class=&quot;kw1&quot;&gt;Print&lt;/span&gt; countof(c1), countof(c2)&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; countof(x &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Object&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Long&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; countof = x.Count&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;That is, if your variables representing objects in your Terminate calls are just REFERENCES to those objects rather than the  original objects themselves, setting them to nothing only frees the reference, not the original object.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Unless your Terminate methods are referring to global public original instances of the parent objects, I suspect your memory leak is akin to the difference between the two numbers in the last line of output from</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Sub</span> test()<br />
&nbsp; <span class="kw1">Dim</span> c1 <span class="kw1">As</span> <span class="kw1">New</span> <span class="kw1">Collection</span>, c2 <span class="kw1">As</span> <span class="kw1">Collection</span><br />
&nbsp; <span class="kw1">Set</span> c2 = c1<br />
&nbsp; Debug.<span class="kw1">Print</span> countof(c1), countof(c2)<br />
&nbsp; c1.Add 1, <span class="st0">&#8220;a&#8221;</span><br />
&nbsp; c1.Add 2, <span class="st0">&#8220;b&#8221;</span><br />
&nbsp; Debug.<span class="kw1">Print</span> countof(c1), countof(c2)<br />
&nbsp; c2.Add 3, <span class="st0">&#8220;c&#8221;</span><br />
&nbsp; Debug.<span class="kw1">Print</span> countof(c1), countof(c2)<br />
&nbsp; <span class="kw1">Set</span> c2 = <span class="kw1">Nothing</span><br />
&nbsp; Debug.<span class="kw1">Print</span> countof(c1), countof(c2)<br />
<span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="kw1">Function</span> countof(x <span class="kw1">As</span> <span class="kw1">Object</span>) <span class="kw1">As</span> <span class="kw1">Long</span><br />
&nbsp; <span class="kw1">On</span> <span class="kw1">Error</span> <span class="kw1">Resume</span> <span class="kw1">Next</span><br />
&nbsp; countof = x.Count<br />
<span class="kw1">End</span> <span class="kw1">Function</span></div>
</div>
<p>That is, if your variables representing objects in your Terminate calls are just REFERENCES to those objects rather than the  original objects themselves, setting them to nothing only frees the reference, not the original object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dick Kusleika</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29652</link>
		<dc:creator>Dick Kusleika</dc:creator>
		<pubDate>Sat, 29 Dec 2007 21:36:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29652</guid>
		<description>&lt;p&gt;Tushar:  If you put a Parent property in Class1 and set it equal to Class2, I think you will get different results.  I agree as long as the dependency only goes one way, VB cleans itself up well.   Make these changes&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;co1&quot;&gt;&#039;Class1&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; Parent &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; Class2&lt;br&gt;
&#160;&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;Class2&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Private&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Class_Initialize()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;ReDim&lt;/span&gt; anObj(499)&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; I &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Integer&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;For&lt;/span&gt; I = &lt;span class=&quot;kw1&quot;&gt;LBound&lt;/span&gt;(anObj) &lt;span class=&quot;kw1&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;UBound&lt;/span&gt;(anObj)&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; anObj(I) = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; Class1&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; anObj(I).Parent = Me&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt; I&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The first blip is the original, the second is never ending.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;http://www.dailydoseofexcel.com/blogpix/garbagecollection.gif&quot; width=&quot;545&quot; height=&quot;525&quot;&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Tushar:  If you put a Parent property in Class1 and set it equal to Class2, I think you will get different results.  I agree as long as the dependency only goes one way, VB cleans itself up well.   Make these changes</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="co1">&#8216;Class1<br />
</span><span class="kw1">Public</span> Parent <span class="kw1">As</span> Class2<br />
&nbsp;<br />
<span class="co1">&#8216;Class2<br />
</span><span class="kw1">Private</span> <span class="kw1">Sub</span> Class_Initialize()<br />
&nbsp; &nbsp; <span class="kw1">ReDim</span> anObj(499)<br />
&nbsp; &nbsp; <span class="kw1">Dim</span> I <span class="kw1">As</span> <span class="kw1">Integer</span><br />
&nbsp; &nbsp; <span class="kw1">For</span> I = <span class="kw1">LBound</span>(anObj) <span class="kw1">To</span> <span class="kw1">UBound</span>(anObj)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Set</span> anObj(I) = <span class="kw1">New</span> Class1<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Set</span> anObj(I).Parent = Me<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Next</span> I<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
<p>The first blip is the original, the second is never ending.</p>
<p><img alt="" src="http://www.dailydoseofexcel.com/blogpix/garbagecollection.gif" width="545" height="525"/></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ross</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29651</link>
		<dc:creator>ross</dc:creator>
		<pubDate>Sat, 29 Dec 2007 14:02:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29651</guid>
		<description>&lt;p&gt;My 2 cents:&lt;br&gt;
&gt;&gt;As far as I know there has never been a need to clean up memory with VB(A)....The Garbage Collector will do the needful.&lt;/p&gt;
&lt;p&gt;?, Surely not Tushar, even in .Net you stil need to call the GC, (often twice!) to control when it runs, if you leave it to the framework, you can be sure when it will run and clear your &quot;dead&quot; objects from memory. &lt;/p&gt;
&lt;p&gt;In VBA I thought it was good practice to set all objects to Nothing when you have finished with them, thus removing them from the memory, but are you saying that VBA classes don&#039;t need this? If the code keeps running then the memory will not be released? will it?&lt;br&gt;
Interesting stuff...&lt;br&gt;
I ran your code Tushar, my results where somewhat different. Looking at the task manager mem usage for Excel.Exe:&lt;br&gt;
opened excel = ~40&#039;000 k&lt;br&gt;
run code     = ~200&#039;000 k&lt;br&gt;
code stops   = ~60&#039;000 k &lt;/p&gt;
&lt;p&gt;But there was large difference in the numbers.&lt;/p&gt;
&lt;p&gt;look forward to seeing what other have to say.&lt;br&gt;
Cheers&lt;br&gt;
Ross&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>My 2 cents:<br />
&gt;&gt;As far as I know there has never been a need to clean up memory with VB(A)&#8230;.The Garbage Collector will do the needful.</p>
<p>?, Surely not Tushar, even in .Net you stil need to call the GC, (often twice!) to control when it runs, if you leave it to the framework, you can be sure when it will run and clear your &#8220;dead&#8221; objects from memory. </p>
<p>In VBA I thought it was good practice to set all objects to Nothing when you have finished with them, thus removing them from the memory, but are you saying that VBA classes don&#8217;t need this? If the code keeps running then the memory will not be released? will it?<br />
Interesting stuff&#8230;<br />
I ran your code Tushar, my results where somewhat different. Looking at the task manager mem usage for Excel.Exe:<br />
opened excel = ~40&#8217;000 k<br />
run code     = ~200&#8217;000 k<br />
code stops   = ~60&#8217;000 k </p>
<p>But there was large difference in the numbers.</p>
<p>look forward to seeing what other have to say.<br />
Cheers<br />
Ross</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tushar Mehta</title>
		<link>http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29648</link>
		<dc:creator>Tushar Mehta</dc:creator>
		<pubDate>Sat, 29 Dec 2007 06:25:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1787#comment-29648</guid>
		<description>&lt;p&gt;Dick,&lt;/p&gt;
&lt;p&gt;As far as I know there has never been a need to clean up memory with VB(A).  Of course, I could be wrong about the historical need but there is absolutely no need to do any cleanup in todays environment.  I suspect that is true for all languages except for unmanaged C++.&lt;/p&gt;
&lt;p&gt;The Garbage Collector will do the needful.&lt;/p&gt;
&lt;p&gt;And, I definitely don&#039;t believe you should be creating and calling a public Terminate method.  Terminate is a *private* method that is automatically invoked by VB.&lt;/p&gt;
&lt;p&gt;In the following test, I nested an object of type Class2 in an object of type Class3.  Each Class2 object had 500 nested object of type Class1, which in turn contained a string variable initialized with a string 1,000,000 characters long.&lt;/p&gt;
&lt;p&gt;There was no cleanup code.&lt;/p&gt;
&lt;p&gt;Memory usage on my PC went from about 766MB to about 1.77GB.  I checked this when the VBA code reached the Stop statement.  Then, with the next step, which terminated execution of the testMemory routine, memory usage dropped back to 766MB.&lt;/p&gt;
&lt;p&gt;I ran the above test thrice.  Very similar results each time.&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;co1&quot;&gt;&#039;Class1&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; aStr &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Private&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Class_Initialize()&lt;br&gt;
&#160; &#160; aStr = &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;(1000000, &lt;span class=&quot;st0&quot;&gt;&quot;a&quot;&lt;/span&gt;)&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;Class2&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; anObj() &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; Class1&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Private&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Class_Initialize()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;ReDim&lt;/span&gt; anObj(499)&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; I &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Integer&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;For&lt;/span&gt; I = &lt;span class=&quot;kw1&quot;&gt;LBound&lt;/span&gt;(anObj) &lt;span class=&quot;kw1&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;UBound&lt;/span&gt;(anObj)&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; anObj(I) = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; Class1&lt;br&gt;
&#160; &#160; &#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt; I&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;Class3&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; anObj2 &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; Class2&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Private&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; Class_Initialize()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; anObj2 = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; Class2&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;co1&quot;&gt;&#039;Module1&lt;br&gt;
&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;Option&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Explicit&lt;/span&gt;&lt;br&gt;
&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt; testMemory()&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; x &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; Class3&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Set&lt;/span&gt; x = &lt;span class=&quot;kw1&quot;&gt;New&lt;/span&gt; Class3&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Stop&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Sub&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
</description>
		<content:encoded><![CDATA[<p>Dick,</p>
<p>As far as I know there has never been a need to clean up memory with VB(A).  Of course, I could be wrong about the historical need but there is absolutely no need to do any cleanup in todays environment.  I suspect that is true for all languages except for unmanaged C++.</p>
<p>The Garbage Collector will do the needful.</p>
<p>And, I definitely don&#8217;t believe you should be creating and calling a public Terminate method.  Terminate is a *private* method that is automatically invoked by VB.</p>
<p>In the following test, I nested an object of type Class2 in an object of type Class3.  Each Class2 object had 500 nested object of type Class1, which in turn contained a string variable initialized with a string 1,000,000 characters long.</p>
<p>There was no cleanup code.</p>
<p>Memory usage on my PC went from about 766MB to about 1.77GB.  I checked this when the VBA code reached the Stop statement.  Then, with the next step, which terminated execution of the testMemory routine, memory usage dropped back to 766MB.</p>
<p>I ran the above test thrice.  Very similar results each time.</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="co1">&#8216;Class1<br />
</span><span class="kw1">Option</span> <span class="kw1">Explicit</span><br />
<span class="kw1">Dim</span> aStr <span class="kw1">As</span> <span class="kw1">String</span><br />
<span class="kw1">Private</span> <span class="kw1">Sub</span> Class_Initialize()<br />
&nbsp; &nbsp; aStr = <span class="kw1">String</span>(1000000, <span class="st0">&#8220;a&#8221;</span>)<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="co1">&#8216;Class2<br />
</span><span class="kw1">Option</span> <span class="kw1">Explicit</span><br />
<span class="kw1">Dim</span> anObj() <span class="kw1">As</span> Class1<br />
<span class="kw1">Private</span> <span class="kw1">Sub</span> Class_Initialize()<br />
&nbsp; &nbsp; <span class="kw1">ReDim</span> anObj(499)<br />
&nbsp; &nbsp; <span class="kw1">Dim</span> I <span class="kw1">As</span> <span class="kw1">Integer</span><br />
&nbsp; &nbsp; <span class="kw1">For</span> I = <span class="kw1">LBound</span>(anObj) <span class="kw1">To</span> <span class="kw1">UBound</span>(anObj)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Set</span> anObj(I) = <span class="kw1">New</span> Class1<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">Next</span> I<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="co1">&#8216;Class3<br />
</span><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Dim</span> anObj2 <span class="kw1">As</span> Class2</p>
<p><span class="kw1">Private</span> <span class="kw1">Sub</span> Class_Initialize()<br />
&nbsp; &nbsp; <span class="kw1">Set</span> anObj2 = <span class="kw1">New</span> Class2<br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></p>
<p><span class="co1">&#8216;Module1<br />
</span><span class="kw1">Option</span> <span class="kw1">Explicit</span></p>
<p><span class="kw1">Sub</span> testMemory()<br />
&nbsp; &nbsp; <span class="kw1">Dim</span> x <span class="kw1">As</span> Class3<br />
&nbsp; &nbsp; <span class="kw1">Set</span> x = <span class="kw1">New</span> Class3<br />
&nbsp; &nbsp; <span class="kw1">Stop</span><br />
&nbsp; &nbsp; <span class="kw1">End</span> <span class="kw1">Sub</span></div>
</div>
]]></content:encoded>
	</item>
</channel>
</rss>

