Reference documentation for deal.II version 9.4.1
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
Functions
MemoryConsumption Namespace Reference

Functions

template<typename T >
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption (const T &t)
 
template<typename T >
std::enable_if<!(std::is_fundamental< T >::value||std::is_pointer< T >::value), std::size_t >::type memory_consumption (const T &t)
 
std::size_t memory_consumption (const char *string)
 
template<typename T >
std::size_t memory_consumption (const std::complex< T > &)
 
template<typename T , std::size_t width>
std::size_t memory_consumption (const VectorizedArray< T, width > &)
 
std::size_t memory_consumption (const std::string &s)
 
template<typename T >
std::size_t memory_consumption (const std::vector< T > &v)
 
template<typename T , std::size_t N>
std::size_t memory_consumption (const std::array< T, N > &v)
 
template<typename T , int N>
std::size_t memory_consumption (const T(&v)[N])
 
std::size_t memory_consumption (const std::vector< bool > &v)
 
template<typename A , typename B >
std::size_t memory_consumption (const std::pair< A, B > &p)
 
template<typename T >
std::size_t memory_consumption (const T *const)
 
template<typename T >
std::size_t memory_consumption (const std::shared_ptr< T > &)
 
template<typename T >
std::size_t memory_consumption (const std::unique_ptr< T > &)
 

Detailed Description

This namespace provides functions helping to determine the amount of memory used by objects. The goal is not necessarily to give the amount of memory used up to the last bit (what is the memory used by a std::map object?), but rather to aid in the search for memory bottlenecks.

This namespace has a single member function memory_consumption() and a lot of specializations. Depending on the argument type of the function, there are several modes of operation:

  1. If the argument is a fundamental C++ data type (such as bool, float, double or any of the integer types), then memory_consumption() just returns sizeof of its argument. The library also provides an estimate for the amount of memory occupied by a std::string.

  2. For objects, which are neither standard types, nor vectors, memory_consumption() will simply call the member function of same name. It is up to the implementation of the data type to provide a good estimate of the amount of memory used. Inside this function, the use of MemoryConsumption::memory_consumption() for compounds of the class helps to obtain this estimate. Most classes in the deal.II library have such a member function.

  3. For vectors and C++ arrays of objects, memory_consumption() recursively calls itself for all entries and adds the results to the size of the object itself. Some optimized specializations for standard data types exist.

  4. For vectors of regular pointers, memory_consumption(T*) returns the size of the vector of pointers, ignoring the size of the objects.

Extending this namespace

The function in this namespace and the functionality provided by it relies on the assumption that there is either a function memory_consumption(T) in this namespace determining the amount of memory used by objects of type T or that the class T has a member function of that name. While the latter is true for almost all classes in deal.II, we have only implemented the first kind of functions for the most common data types, such as fundamental types, strings, C++ vectors, C-style arrays, and C++ pairs. These functions therefore do not cover, for example, C++ maps, lists, etc. If you need such functions feel free to implement them and send them to us for inclusion.

Wells

Function Documentation

◆ memory_consumption() [1/14]

template<typename T >
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type MemoryConsumption::memory_consumption ( const T &  t)
inline

Calculate the memory consumption of a fundamental type. See EnableIfScalar for a discussion on how this restriction (SFINAE) is implemented.

Definition at line 266 of file memory_consumption.h.

◆ memory_consumption() [2/14]

template<typename T >
std::enable_if<!(std::is_fundamental< T >::value||std::is_pointer< T >::value), std::size_t >::type MemoryConsumption::memory_consumption ( const T &  t)
inline

Estimate the memory consumption of an object. If no further template specialization (past this one) is available for the type T, then this function returns the member function t.memory_consumption()'s value.

Definition at line 417 of file memory_consumption.h.

◆ memory_consumption() [3/14]

std::size_t MemoryConsumption::memory_consumption ( const char *  string)
inline

Determine the amount of memory consumed by a C-style string. The returned value does not include the size of the pointer. This function only measures up to (and including) the NUL byte; the underlying buffer may be larger.

Definition at line 274 of file memory_consumption.h.

◆ memory_consumption() [4/14]

template<typename T >
std::size_t MemoryConsumption::memory_consumption ( const std::complex< T > &  )
inline

Determine the amount of memory in bytes consumed by a std::complex variable.

Definition at line 290 of file memory_consumption.h.

◆ memory_consumption() [5/14]

template<typename T , std::size_t width>
std::size_t MemoryConsumption::memory_consumption ( const VectorizedArray< T, width > &  )
inline

Determine the amount of memory in bytes consumed by a VectorizedArray variable.

Definition at line 299 of file memory_consumption.h.

◆ memory_consumption() [6/14]

std::size_t MemoryConsumption::memory_consumption ( const std::string &  s)
inline

Determine an estimate of the amount of memory in bytes consumed by a std::string variable.

Definition at line 307 of file memory_consumption.h.

◆ memory_consumption() [7/14]

template<typename T >
std::size_t MemoryConsumption::memory_consumption ( const std::vector< T > &  v)
inline

Determine the amount of memory in bytes consumed by a std::vector of elements of type T by calling memory_consumption() for each entry.

This function loops over all entries of the vector and determines their sizes using memory_consumption() for each v[i]. If the entries are of constant size, there might be another global function memory_consumption() for this data type or if there is a member function of that class of that names that returns a constant value and the compiler will unroll this loop so that the operation is fast. If the size of the data elements is variable, for example if they do memory allocation themselves, then the operation will necessarily be more expensive.

Using the algorithm, in particular the loop over all elements, it is possible to also compute the memory consumption of vectors of vectors, vectors of strings, etc, where the individual elements may have vastly different sizes.

Note that this algorithm also takes into account the size of elements that are allocated by this vector but not currently used.

For the most commonly used vectors, there are special functions that compute the size without a loop. This also applies for the special case of vectors of bools.

Definition at line 316 of file memory_consumption.h.

◆ memory_consumption() [8/14]

template<typename T , std::size_t N>
std::size_t MemoryConsumption::memory_consumption ( const std::array< T, N > &  v)
inline

Determine the amount of memory in bytes consumed by a std::array of N elements of type T by calling memory_consumption() for each entry.

This function loops over all entries of the array and determines their sizes using memory_consumption() for each v[i]. If the entries are of constant size, there might be another global function memory_consumption() for this data type or if there is a member function of that class of that names that returns a constant value and the compiler will unroll this loop so that the operation is fast. If the size of the data elements is variable, for example if they do memory allocation themselves, then the operation will necessarily be more expensive.

Using the algorithm, in particular the loop over all elements, it is possible to also compute the memory consumption of arrays of vectors, arrays of strings, etc, where the individual elements may have vastly different sizes.

Definition at line 339 of file memory_consumption.h.

◆ memory_consumption() [9/14]

template<typename T , int N>
std::size_t MemoryConsumption::memory_consumption ( const T(&)  v[N])
inline

Estimate the amount of memory (in bytes) occupied by a C-style array. Since in this library we do not usually store simple data elements like doubles in such arrays (but rather use std::vectors or deal.II Vector objects), we do not provide specializations like for the std::vector arrays, but always use the loop over all elements.

Definition at line 359 of file memory_consumption.h.

◆ memory_consumption() [10/14]

std::size_t MemoryConsumption::memory_consumption ( const std::vector< bool > &  v)
inline

Specialization of the determination of the memory consumption of a vector, here for a vector of bools.

This is a special case, as the bools are not stored one-by-one, but as a bit field.

Definition at line 370 of file memory_consumption.h.

◆ memory_consumption() [11/14]

template<typename A , typename B >
std::size_t MemoryConsumption::memory_consumption ( const std::pair< A, B > &  p)
inline

Determine an estimate of the amount of memory in bytes consumed by a pair of values.

Definition at line 379 of file memory_consumption.h.

◆ memory_consumption() [12/14]

template<typename T >
std::size_t MemoryConsumption::memory_consumption ( const T * const  )
inline

Calculate the memory consumption of a pointer.

Note
This function is overloaded for C-style strings; see the documentation of that function for that case.
This returns the size of the pointer, not the size of the object pointed to.

Definition at line 388 of file memory_consumption.h.

◆ memory_consumption() [13/14]

template<typename T >
std::size_t MemoryConsumption::memory_consumption ( const std::shared_ptr< T > &  )
inline

Return the amount of memory used by a shared pointer.

Note
This returns the size of the pointer, not of the object pointed to.

Definition at line 397 of file memory_consumption.h.

◆ memory_consumption() [14/14]

template<typename T >
std::size_t MemoryConsumption::memory_consumption ( const std::unique_ptr< T > &  )
inline

Return the amount of memory used by a std::unique_ptr object.

Note
This returns the size of the pointer, not of the object pointed to.

Definition at line 406 of file memory_consumption.h.