Open In App

C++17 – <memory_resource> Header

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

C++17 introduced the <memory_resource> header, which provides a mechanism for customizing the allocation and deallocation of memory in C++ programs. This header defines the memory_resource class and several derived classes that implement different memory allocation strategies. The C++ Standard Library has incorporated the header which can be accessed by including the header. You need to explicitly add the header in order to use it. In C++, memory allocation is usually achieved via the new and delete operators provided by the language. These operators are accountable for allocating and releasing memory to objects but are not always functional in every scenario.

The interface for allocating and deallocating memory is defined by the abstract base class memory_resource class. The design of fresh memory allocation methods can be achieved by crafting personalized algorithms for allocating and deallocating while exploiting the memory_resource category. One useful aspect found in this class is the is_equal function, allowing for the comparison of two memory_resource entities to verify any similarities. This feature proves particularly beneficial when confirming whether memory allocators follow the same allocation techniques.

Memory Resource class

The memory_resource class, the <memory_resource> header defines several derived classes that implement different allocation strategies. These classes include:

  • The new_delete_resource class uses the new and delete operators to allocate and deallocate memory.
  • The null_memory_resource class does not allocate any memory and always returns nullptr.
  • The monotonic_buffer_resource class allocates memory from a pre-allocated buffer.
  • The class known as pool_resource is responsible for memory allocation from a pool of memory.

Syntax:

#include <memory_resource>

Examples of Memory  Resource

Example 1: 

C++




// C++ Program to demonstrate the use of the
// <memory_resource> Header.
#include <iostream>
#include <memory_resource>
#include <vector>
  
int main()
{
    std::pmr::monotonic_buffer_resource buffer(1024);
    std::pmr::vector<int> v(&buffer);
    v.push_back(42);
  
    for (const auto& element : v) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
  
    return 0;
}


Output:

42

Example 2: 

C++




// C++ Program to demonstrate the use of the
// <memory_resource> Header.
#include <iostream>
#include <memory_resource>
#include <vector>
  
int main()
{
    std::pmr::unsynchronized_pool_resource pool;
    std::pmr::vector<int> v(&pool);
  
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
  
    std::cout << "Vector elements: ";
    for (const auto& element : v) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
  
    return 0;
}


Output:

Vector elements: 0 1 2 3 4 5 6 7 8 9 

When we utilize an unsynchronized_pool_resource to manage the vector, we can skip the heap allocations and make use of a memory pool for assigning memory to the components of the vector which may come in handy when there is a need to frequently assign or withdraw memory. Employing a memory pool can be more effective than the conventional method of allocating and withdrawing memory from the heap.

Advantages of using memory_resource header

  • Custom Memory Allocation: By utilizing the memory_resource header, you can generate personalized memory allocators that function in conjunction with any STL container. This facilitates the refinement of memory allocation for particular use cases, thereby decreasing excess and enhancing performance.
  • Memory Reuse: The usage of the memory_resource header allows for the recycling of previously allocated memory, ultimately resulting in better performance by minimizing the need to repeatedly allocate and deallocate memory.
  • Reduced Fragmentation: Improving overall performance can be achieved by reducing memory fragmentation. This happens when blocks of allocated memory are separated by small, unused gaps. A solution to this issue is to implement custom allocators that manage memory more efficiently.
  • Improved Memory Profiling: You can utilize the memory_resource header to profile the memory usage of your application. This feature assists you in recognizing and addressing troubles that can adversely affect performance like memory leaks and overconsumption of memory.
  • Compatibility: Modern C++ compilers widely support the memory_resource header, which is included in the C++17 standard. With this knowledge, developers can confidently utilize it in their code, as it will be compatible with various C libraries and tools.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads