How can I track C++’s memory allocation module-wise by Allocator?

I have a large C++ program which uses much of the standard libraries. It also uses C-style allocation malloc much. I want to analyze how much memory is consumed in one module.

In short, Mgrx are all very complicated classes. They consist of various fields, in which the standard libraries and custom-defined classes are widely used. Now I want to check how much memory Module1 consumes. I want to it less manually.

struct Mgr1;

struct Mgr2;

struct Mgr3;


struct Module1 {
    vector<Mgr1> v1;
    vector<Mgr1> v3;
    ...
};


struct Module2 {
    vector<Mgr1> v2;
    vector<Mgr1> v3;
};

Generally, there are several utils for problem:

  1. Jemalloc
    Which means I can use thread.allocatedp to track thread-wise allocation. If my program is moduled by thread pools(which means every module uses its own dedicated thread pool to consume requests, and communicate with each other by sending messages), then it is a good idea.
  2. mmap/sbrk
    I have totally got rid of directly calling mmap or sbrk for memory allocations. So all memory allocations are “delegated” by jemalloc
  3. The arena of Jemalloc
    • We can bind some threads to their dedicated arena, so the size of each arena represents memory “owned” by this thread.
    • If the program is not moduled by thread pools, then we can use the mallocx in specific modules. For example, given a module A which calls module B. If we want to compute how much memory module B consumes, then we can replace all malloc with mallocx, providing a MALLOCX_ARENA which specifies a dedicated arena for module B. Thus, the size of this arena is the memory “owned” by module B.

Aboving is general ideas of how to track memory thread-wise or module-wise. However, it is difficult to implement when using C++. Because:

  1. C++’s standard libraries uses Allocator to specify how to allocate memories. We can’t just replace malloc with mallocx by some macro magic. We have to defined our own Allocator. It is kind of difficult, even the simplest std::string should be replaced with std::basic_string<char, std::char_traits<char>, Myallocator<char>> s.
  2. Allocators have types, vector<T, ThisAllocator<T>> differs from vector<T, ThatAllocator<B>>. Moreover, if we have some vector<vector<T>> or ever complicated types, we have to use scoped_allocator_adaptor.

As a result, it is very hard to track memory allocation of all instances of a certain class or module.

I wonder if it is some easier ways that won’t reduce the performance too much? IMO, if every fields and sub-fields of Mgr1 uses some kind of Allocator, then I can record memory usage in this allocator’s allocate method. However, how can I enforce a custom Allocator for and only for Mgr1 and all its direct or indirect sub-fields?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật