C++17 - <memory_resource> Header
Last Updated :
10 May, 2023
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.
Similar Reads
C++23 <print> Header C++ has long been a powerful language, known for its versatility and performance. With each new standard release, the language evolves, introducing features that enhance developer productivity and code readability. One of these additions of the C++ 23 standard is the introduction of <print> he
3 min read
C++17 - <charconv> Header The C++ <charconv> header provides many functions for converting the character sequences to numerical values and vice-versa. It is considered better than the <cstdlib> header file functions for the same purpose. The functions provided by the <charconv> header file are generally fas
5 min read
C++ 11 - <atomic> Header In C++11, the <atomic> header introduces a powerful mechanism for managing concurrent access to shared data in multithreaded applications. This header provides atomic types and operations that ensure safe access to variables, preventing data races and potential issues in multithreaded code. In
4 min read
C++ 11 - <cstdint> Header <cstdint> header in C++ ensures the portability of integer types with specialized width and signedness across various systems. The absence of standardization for integer types caused issues in coding and constructing portable programs before the advent of. In this article, we will explore the
3 min read
Memory Model in C++ 11 Memory Model is a specification that describes how the program interacts with the memory. In C++ 11, a standardized memory model is created to provide the solution to issues surrounding concurrency, ordering, and multithreading. This framework specifies how memory is accessed and arranged in a C++ p
5 min read
C++ 11 - <cinttypes> Header The header offers type aliases and functions for manipulating integer types with particular sizes and signedness, as a part of the C++11 standard. Its aim is to present a standard group of integer types across diverse platforms and architectures. <cinttypes> header The <cinttypes> header
2 min read