Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-24
4
Medium Priority
?
118 Views
Last Modified: 2015-02-11
How much memory requires class object? How to determine that?
0
Comment
Question by:Nusrat Nuriyev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1332 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 668 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 1332 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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