memory allocation is slow, why and how to improve

kawa kawa at none.com
Sun Oct 22 02:36:22 PDT 2006


Andy Knowles wrote:
> When you say memory allocation should be fast, are you referring to just 
> memory allocation or memory allocation and deallocation (memory 
> management)?
> 
> If you are talking about only memory allocation, you should not be 
> deleting the memory you allocated in C++ and you should be disabling the 
> garbage collector in D and Java.  I'm not sure this is possible in Java.
Right, don't use GC if you (moslty) manage memory manually.
You can't turn off GC in Java.

> Java may be faster than D because the garbage collector is not even 
> called - the program simply exits and leaves cleanup to the OS.  This 
> would make it faster than C++ because there is no need to delete.

The java VM don't run GC collect on exit (.NET clr do).
Add -verbose:gc flag

> Java probably also has a smarter GC than D.  D's GC is not type-aware, 
> so it has to scan the arrays you allocate for pointers to other objects. 
>  This is one area where D's GC could be improved.
As far as I know D use a conservative GC, being able to have a 
generational one like Java improve performance in such typical situations.

> 
> D also initializes allocated arrays where C++ does not.  I believe Java 
> does as well but I'm not sure.
Java initializes every data so you have to do the same in C++.

> 
> And last of all, you should be careful constructing examples that do 
> nothing as a clever optomiser will get rid of the parts that cannot 
> possibly have any side effects.
> 
> I'd be interested to see timing results for more carefully constructed 
> examples.
Me too :)

> 
> Regards,
> Andy



More information about the Digitalmars-d mailing list