Thanks for the lengthy reply. I need to start studying GC&#39;s before I assume more things like that. :)<br><br><div class="gmail_quote">On Tue, Aug 10, 2010 at 3:11 AM, Jonathan M Davis <span dir="ltr">&lt;<a href="mailto:jmdavisprog@gmail.com">jmdavisprog@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">On Monday, August 09, 2010 17:07:28 Andrej Mitrovic wrote:<br>
&gt; I don&#39;t have a lot of experience in this field, but my guess is people want<br>
&gt; to help out the GC be more efficient in some regard.<br>
&gt;<br>
&gt; &quot;Hey, GC! I know you take the trash out every day, but I just want to let<br>
&gt; you know that I&#39;ve cleaned up my room and there&#39;s a whole bag o&#39; dirt in<br>
&gt; here ready to be thrown out that you should know about. See ya later!&quot;<br>
&gt;<br>
&gt; My assumption is that the programmer probably knows when and how he uses<br>
&gt; memory more so than the GC itself. So my thinking is some kind of hybrid<br>
&gt; approach would be a good choice between safety and performance here. But<br>
&gt; correct me if I&#39;m wrong, I&#39;m walking in the dark really..<br>
<br>
</div>A GC is tuned with the idea that it will run periodically and free memory which<br>
is freeable at that point rather than frequently freeing it at the programmer&#39;s<br>
request like happens with manually managed memory. And since freeing memory can<br>
be expensive, it can be argued that it&#39;s actually more efficient to just let the<br>
memory not be freed until the garbage collector does its thing whenever that is.<br>
I&#39;ve heard that there have been papers showing that that&#39;s more efficient, but I<br>
haven&#39;t read them. The issue is if the time that the garbage collector decides<br>
to run is when you&#39;re trying to do something time or resource-sensitive, and the<br>
garbage collector picks a bad time to run. But that&#39;s generally a fact of life<br>
with garbage collectors, and if it&#39;s an efficient garbage collector, then<br>
hopefully the hit is minimal.<br>
<br>
Also, it&#39;s not like the garbage collector is likely to be really freeing your<br>
memory anyway, unless it decides that it just has way too much allocated. So,<br>
telling it that you want it to free something really is only getting the<br>
destructor called for you, which likely only matters if you have another<br>
resource of some kind (like file handles or something) which you need freed. And<br>
if that&#39;s what you need, you could just as easily call a function on the class<br>
to free that up as go and completely destroy it. The one thing that I can think<br>
of that clear() might get for you otherwise is helping the GC know that it any<br>
other references that that class holds aren&#39;t needed anymore, but if you no<br>
longer have any references to the object in question, the GC should already have<br>
that information.<br>
<br>
Really, in the general case, it&#39;s going to be most efficient to let the GC do what<br>
it does. If it&#39;s well-written, it should do it efficiently. Trying to tell it that<br>
you know best is not likely to work well (in the general case, at least). If you<br>
really want to be freeing things as soon as you&#39;re done with them, you need to<br>
do manually memory management. Besides&#39; s soon as you&#39;re keeping track of when<br>
it would be okay to clear() an object, that&#39;s pretty much what you&#39;re doing<br>
anyway.<br>
<font color="#888888"><br>
- Jonathan M Davis<br>
</font></blockquote></div><br>