rapidcoder wrote: |
---|
Another problem is that you cannot move objects in memory and you cannot find all pointers pointing to the given block of memory. |
I am not expert on gc-s, but AFAIK even built-in gc will have to work for that. It is a processing intensive graph algorithm no matter how you look at it. Even if you write it in assembler.
And another problem is that you can have pointers pointing to the inside of the allocated block, not only to its beginning (because of multiple inheritance and pointer arithmetic). |
This is a problem particularly in C++ due to lack of any portable way to test whether a pointer is directed inside a given range. I think I did mention that in the previous post. Also, only POD types have layout specification in the standard. For me this is just plain lacking, because some constraints are natural, such as laying out all sub-objects inside the memory range of the outer object.
And another problem are destructors - you have to call them, and C++ code is usually littered with destructors everywhere. |
Destructors or finalization, you've got to have something. The problem is that there is fundamental difference between memory management and resource management in general. When I say fundamentally, I don't mean in particular design, I mean in its very nature. If I understand correctly, clean-up in systems where the references create graphs with cycles is generally something complex. But this is not problem particularly for C++.
And fall back to semi-manual management is not entirely satisfactory. Say object X keeps a socket open, and object Y owns object X. Then I have to say to the users of object X, and of object Y, that both require their clean-up ASAP. My designs will not be very transparent to the users of those types. What if I start using some pooling method in the future, and the clean-up is not anymore so critical. Will anyone modify their code to reflect the change and to tidy their functions.
All of this is a reason why precise GC in C++ would be extremely costly compared to GC in systems designed from ground up for it |
They will be a bit costlier for sure. Like matrix computations compared to Matlab. Or regular expressions compared to Python and Perl. But does this mean that we will never build libraries - we will just wait for compiler developers to integrate every feature we need in the language. And I don't like vendor lock-in. Also, I admit that without compile time type introspection, the presence of several pointers in a single object would be handled sub-optimally in C++, but who knows - this may change.
you just lose most of benefits given by modern generational GCs: fast allocation of small objects, 0-cost deallocation, lack of heap fragmentation and good memory reference locality |
I see what you mean. You mean compacting gc. Compacting gc-s have numerous benefits, because they use memory stacks and this makes the allocation rapid. The problem is not that you can not create compacting gc in C++, the problem is that you can not efficiently create growing stack in C++. And the core problem in doing that is the lack of separate reserve and commit operations in the language. You need to be able to reserve big range of memory and commit actual resource for it only when it is later needed. This is also omission. One can try to use vector presently, if careful, but it wont be as efficient. On the other hand you may not be comfortable with the objects moving in memory for some reason - like interprocess communication.
transparency and ease of use for programmer. |
This is always a problem with extensions implemented by the programmer. But instead of deciding over the need to have more features in the language, I prefer entirely new paradigms - aspect oriented programming, language oriented programming, etc. I know this is day dreaming at the moment, but I look to the future with hope.
As I already said, I really would like to have gc as part of the run-time, not as part of the platform. If the run-time wants to delegate the support to the platform for efficiency, fine. I still want to be able to compile for freestanding environments though.
Regards