Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the memory used by any object

Tags:

class Help { public:         Help();         ~Help();          typedef std::set<string> Terms;         typedef std::map<string, std::pair<int,Terms> > TermMap;         typedef std::multimap<int, string, greater<int> > TermsMap;  private:          TermMap  terms;         TermsMap    termsMap; }; 

How can we find the memory used (in bytes) by the objects term and termsMap. Do we have any library ?

like image 582
ARV Avatar asked Jan 29 '10 06:01

ARV


People also ask

How much memory does an object use?

In a modern 64-bit JDK, an object has a 12-byte header, padded to a multiple of 8 bytes, so the minimum object size is 16 bytes. For 32-bit JVMs, the overhead is 8 bytes, padded to a multiple of 4 bytes. (From Dmitry Spikhalskiy's answer, Jayen's answer, and JavaWorld.)

How do you measure memory usage?

Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.

How do you check the memory of a Python object?

In python, the usage of sys. getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes.

How much memory is occupied by a class?

Simply a class without an object requires no space allocated to it. The space is allocated when the class is instantiated, so 1 byte is allocated by the compiler to an object of an empty class for its unique address identification. If a class has multiple objects they can have different unique memory locations.


1 Answers

If you are looking for the full memory usage of an object, this can't be solved in general in C++ - while we can get the size of an instance itself via sizeof(), the object can always allocate memory dynamically as needed.

If you can find out how big the individual element in a container are, you can get a lower bound:

size = sizeof(map<type>) + sum_of_element_sizes; 

Keep in mind though that the containers can still allocate additional memory as an implementation detail and that for containers like vector and string you have to check for the allocated size.

like image 61
Georg Fritzsche Avatar answered Nov 04 '22 02:11

Georg Fritzsche