Solved

22. How much memory requires class object? How to determine that?

Posted on 2015-01-24
4
99 Views
Last Modified: 2015-02-11
How much memory requires class object? How to determine that?
0
Comment
Question by:Nusrat Nuriyev
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 333 total points
ID: 40568249
The net size of a class can be obtained using 'sizeof()', just like in C, e.g.

#include <iostream>

class Vehicle {
public:
  void Drive() {speed = 42;}
protected:
  int speed;
};

int main() {

  std::cout << "Sizeo of Vehicle is " << sizeof(Vehicle) << std::endl;
  return 0;
}

Open in new window


But, there's a caveat: When a class contains pointer members whose values are allocated dynamically, that memory isn't taken into account by 'sizeof()' - you would have to provide a member that does account for that memory and reports it.
The same applies to classes that use lists or other containers.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 167 total points
ID: 40568684
sizeof gives you the macroscopic answer. To optimize your performance you may wish to take a finer look at the reasons your sizeof result may be higher than desired. You may wish to look closer at the cache alignment and data holes in the class data members. To do this use the Linux pahole utility:
http://linux.die.net/man/1/pahole

The goal is to rearrange your data members to minimize the cache lines, to try to keep data members from requiring two cache lines, and to reduce the number of holes in your data members (i.e., wasted portions of cache lines).
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40604640
Jkr,
The same applies to classes that use lists or other containers.
Did you mean stl containers , iterators?

But, there's a caveat: When a class contains pointer members whose values are allocated dynamically, that memory isn't taken into account by 'sizeof()' - you would have to provide a member that does account for that memory and reports it.

Likewise, usual pointer? it always give's us 4 bytes, right?

Good answer, phoffric!
But still this does not solve the problem of dynamic member data allocation size affected when determining size?
Is it possible to get this info?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 333 total points
ID: 40604674
>> Did you mean stl containers , iterators?

For example, since these are the most common ones. Or, let's say: std::string

>> Likewise, usual pointer? it always give's us 4 bytes, right?

Yes, exactly. But when you dynamically allocate memory to it, you would have to 'manually' account for that. On the other hand, if you argue that this memory is not 'within' the object... but that is a bit short handed, since teh consumption still persists.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 91
Problem with SqlConnection 4 178
Path to  STL Map header file 1 70
C++ mouse_event mouse look 7 97
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question