Faster Virtual Method Dispatch

James Dunne james.jdunne at gmail.com
Wed Apr 26 11:22:31 PDT 2006


xs0 wrote:
> 
>> Not true.  A hierachical memory management system is great!  It sure 
>> beats reference counting, manual memory management, and conservative GC.
>>
>> What you do is allocate some memory, but also provide a "parent" 
>> memory pointer to relate the new memory to.  When all is said and done 
>> you have a nice tree of related memory pointers (hopefully with just 
>> one root, provided you designed correctly).
>>
>> To free some memory, simply walk all the branches of that node, 
>> freeing their memory, then free the original memory.  You can even get 
>> predictable destructors for objects this way.
>>
>> I'm currently developing a project in C which uses this method.  I 
>> have it allocating lots of small blocks of memory to perform a job.  
>> The total comes to 32KB.  When the program is finished I do one "free" 
>> call to the root structure and I get back ALL of my memory.  Not a 
>> single byte is leaked.
> 
> 
> But how can you tell _when_ to delete the root? AFAIK, the main benefit 
> of GC is not that you don't have to manually free the memory, but that 
> you don't have to know when it is safe to do so. I don't see how 
> organizing memory blocks into a tree solves that...
> 
> 
> xs0

That's a different problem and depends on the nature of the application. 
  If you're in a server environment, you free memory when the client 
request has been completely processed.  If you're in an interactive 
user-interface environment, you free memory when you close documents, 
close windows, etc.  When the data is no longer needed, you'll know when 
to free and what to free.

I think you thought I was implying you should only have one memory root 
which all things are related to and that is the only thing that should 
ever be freed.  The first part is right, but not the second.  You should 
keep all the memory related to each other, but it's not that you have to 
free it in an "all or nothing" circumstance.  Then again, that depends 
on your application type.

In my example application I have lots of memory free operations on the 
individual nodes when I know I won't need that memory anymore, such as 
for temporary strings.  If I forget to include an explicit free call to 
that temporary memory, it's okay since I know that memory has been 
related to another parent memory block that _will_ be freed.

Can you provide me a few legitimate examples of when you won't know what 
to free and when to free it?  I'm having a tough time coming up with 
examples on my own...

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O 
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e 
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------

James Dunne



More information about the Digitalmars-d mailing list