Memory Allocation/Deallocation Bottleneck?

后端 未结 12 1987
轮回少年
轮回少年 2020-11-30 20:44

How much of a bottleneck is memory allocation/deallocation in typical real-world programs? Answers from any type of program where performance typically matters are welcome.

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 21:32

    This is where c/c++'s memory allocation system works the best. The default allocation strategy is OK for most cases but it can be changed to suit whatever is needed. In GC systems there's not a lot you can do to change allocation strategies. Of course, there is a price to pay, and that's the need to track allocations and free them correctly. C++ takes this further and the allocation strategy can be specified per class using the new operator:

    class AClass
    {
    public:
      void *operator new (size_t size); // this will be called whenever there's a new AClass
       void *operator new [] (size_t size); // this will be called whenever there's a new AClass []
      void operator delete (void *memory); // if you define new, you really need to define delete as well
      void operator delete [] (void *memory);define delete as well
    };
    

    Many of the STL templates allow you to define custom allocators as well.

    As with all things to do with optimisation, you must first determine, through run time analysis, if memory allocation really is the bottleneck before writing your own allocators.

提交回复
热议问题