Size of an object in memory at runtime

Has anyone here successfully (and accurately) determined the size of a C++ class instance (Object) in memory during run time? _______________________________________________ *Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ * *

@James, a very nice question. When I was reading into OOP in C#, this was my primary concern for many months as they get stored in the heap. Variables, arrays etc holds much less in the stack, but then also learnt that memory these days does not cost the price of a computer as the early days so it was not a major issue. However, I once did see a single program take up 100% of the memory, yet the OS was only taking up less than 15% due to the nature of GUI and I refused to purchase the software. My apprentice view. :-) On Mon, Nov 28, 2011 at 10:56 AM, James Nzomo <kazikubwa@gmail.com> wrote:
Has anyone here successfully (and accurately) determined the size of a C++ class instance (Object) in memory during run time?

In C++ to determine the size of a class in memory you look at the variables linked to the class so take for example: class myClass { int x = 2; int y = 3; const int z = 10; myClass(int a, int b) { x = a; y = b; } int addInt() { return x+y; } } The size of this class instance in memory is basically the size of the two variables x and y which would be about 8 bytes total plus some overhead which could be a single byte. Notice the constant int z. This is not part of the instance of a C++ class. Reasoning is even if you make ten instances of the class all you will have is 10 instances of x and 10 instances of y which is total 80 bytes. z will only be one instance. Thats why you can use z without creating an object of the class. The object is stored in the heap with the values and the pointer to that object is stored in the stack eg MyClass happy = new MyClass(10, 20); happy is the address of "new MyClass(10, 20)" is stored in the stack and takes up 1 byte (lets assume the underlying pointer is stored in a single byte) "MyClass(10, 20)" is stored in the heap and takes up 10 x sizeof(int) + 20 x sizeof(int) + overhead. There is an argument about the memory of member functions. They behave the same way. calculate the size of the local variables in the member functions. Stuff like for loops, while, if else. those are language constructs and are handled internally by the language hence the overhead. It is slightly more complicate than this but I hope you get the idea behind memory management in C++ and C also (structs and unions) Steve ----- Original Message ----- | From: "aki" <aki275@gmail.com> | To: "Skunkworks Mailing List" <skunkworks@lists.my.co.ke> | Sent: Monday, November 28, 2011 11:13:07 AM | Subject: Re: [Skunkworks] Size of an object in memory at runtime | @James, a very nice question. When I was reading into OOP in C#, this | was my primary concern for many months as they get stored in the | heap. Variables, arrays etc holds much less in the stack, but then | also learnt that memory these days does not cost the price of a | computer as the early days so it was not a major issue. However, I | once did see a single program take up 100% of the memory, yet the OS | was only taking up less than 15% due to the nature of GUI and I | refused to purchase the software. My apprentice view. :-) | On Mon, Nov 28, 2011 at 10:56 AM, James Nzomo < kazikubwa@gmail.com > | wrote: | _______________________________________________ | Skunkworks mailing list | Skunkworks@lists.my.co.ke

Oops slight correction I made some grave errors I was a bit too generous in my math :) corrections 10 x sizeof(int) + 20 x sizeof(int) + overhead should be sizeof(int) + sizeof(int) + overhead 10 instances of x and 10 instances of y which is total 80 bytes. should be 1 instance of x and one instance of y = 8 bytes not 80 bytes ----- Original Message ----- | From: "Steve Obbayi" <steve@sobbayi.com> | To: "Skunkworks Mailing List" <skunkworks@lists.my.co.ke> | Sent: Monday, November 28, 2011 11:49:34 AM | Subject: Re: [Skunkworks] Size of an object in memory at runtime | In C++ to determine the size of a class in memory you look at the | variables linked to the class | so take for example: | class myClass | { | int x = 2; | int y = 3; | const int z = 10; | myClass(int a, int b) | { | x = a; | y = b; | } | int addInt() | { | return x+y; | } | } | The size of this class instance in memory is basically the size of | the two variables x and y which would be about 8 bytes total plus | some overhead which could be a single byte. | Notice the constant int z. This is not part of the instance of a C++ | class. Reasoning is even if you make ten instances of the class all | you will have is | 10 instances of x and 10 instances of y which is total 80 bytes. z | will only be one instance. Thats why you can use z without creating | an object of the class. | The object is stored in the heap with the values and the pointer to | that object is stored in the stack eg | MyClass happy = new MyClass(10, 20); | happy is the address of "new MyClass(10, 20)" is stored in the stack | and takes up 1 byte (lets assume the underlying pointer is stored in | a single byte) | "MyClass(10, 20)" is stored in the heap and takes up 10 x sizeof(int) | + 20 x sizeof(int) + overhead. | There is an argument about the memory of member functions. They | behave the same way. calculate the size of the local variables in | the member functions. Stuff like for loops, while, if else. those | are language constructs and are handled internally by the language | hence the overhead. | It is slightly more complicate than this but I hope you get the idea | behind memory management in C++ and C also (structs and unions) | Steve | ----- Original Message ----- | _______________________________________________ | Skunkworks mailing list | Skunkworks@lists.my.co.ke

Thank you aki, that was a very helpful insight (really). Are there any C++ 1337s on the list who have encountered this problem and solved it? I am trying to avoid painstaking workarounds such as adding up the size of each member or using JSON. Anyone? _______________________________________________ *Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ * * 2011/11/28 aki <aki275@gmail.com>
@James, a very nice question. When I was reading into OOP in C#, this was my primary concern for many months as they get stored in the heap. Variables, arrays etc holds much less in the stack, but then also learnt that memory these days does not cost the price of a computer as the early days so it was not a major issue. However, I once did see a single program take up 100% of the memory, yet the OS was only taking up less than 15% due to the nature of GUI and I refused to purchase the software. My apprentice view. :-)
On Mon, Nov 28, 2011 at 10:56 AM, James Nzomo <kazikubwa@gmail.com> wrote:
Has anyone here successfully (and accurately) determined the size of a C++ class instance (Object) in memory during run time?
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Sorry folks, hadn't seen the other replies. @Steve Adding up the sizes of vars is what i was trying to avoid. I'm just trying to find an elegant solving the above @Josh sizeof() doesn't give the size of the object at runtime (in memory). _______________________________________________ *Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ * * 2011/11/28 James Nzomo <kazikubwa@gmail.com>
Thank you aki, that was a very helpful insight (really). Are there any C++ 1337s on the list who have encountered this problem and solved it? I am trying to avoid painstaking workarounds such as adding up the size of each member or using JSON.
Anyone?
_______________________________________________
*Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ *
*
2011/11/28 aki <aki275@gmail.com>
@James, a very nice question. When I was reading into OOP in C#, this was my primary concern for many months as they get stored in the heap. Variables, arrays etc holds much less in the stack, but then also learnt that memory these days does not cost the price of a computer as the early days so it was not a major issue. However, I once did see a single program take up 100% of the memory, yet the OS was only taking up less than 15% due to the nature of GUI and I refused to purchase the software. My apprentice view. :-)
On Mon, Nov 28, 2011 at 10:56 AM, James Nzomo <kazikubwa@gmail.com>wrote:
Has anyone here successfully (and accurately) determined the size of a C++ class instance (Object) in memory during run time?
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

What i'm trying to do is implement drag & drop clipboard opps for a custom data object (a custom class/object in this case) that works accross multiple instances of the same running application. In order to do this efficiently, i have to allocate the exact amount of memory required to store the class instance as a D&D object. The other alternatives would be to give a generous over estimate of the required memory or to use the objects JSON string as the D&D object. But the latter requires alot of coding overhead. _______________________________________________ *Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ * * 2011/11/28 James Nzomo <kazikubwa@gmail.com>
Sorry folks, hadn't seen the other replies.
@Steve Adding up the sizes of vars is what i was trying to avoid. I'm just trying to find an elegant solving the above
@Josh sizeof() doesn't give the size of the object at runtime (in memory).
_______________________________________________
*Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ *
*
2011/11/28 James Nzomo <kazikubwa@gmail.com>
Thank you aki, that was a very helpful insight (really). Are there any C++ 1337s on the list who have encountered this problem and solved it? I am trying to avoid painstaking workarounds such as adding up the size of each member or using JSON.
Anyone?
_______________________________________________
*Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ *
*
2011/11/28 aki <aki275@gmail.com>
@James, a very nice question. When I was reading into OOP in C#, this was my primary concern for many months as they get stored in the heap. Variables, arrays etc holds much less in the stack, but then also learnt that memory these days does not cost the price of a computer as the early days so it was not a major issue. However, I once did see a single program take up 100% of the memory, yet the OS was only taking up less than 15% due to the nature of GUI and I refused to purchase the software. My apprentice view. :-)
On Mon, Nov 28, 2011 at 10:56 AM, James Nzomo <kazikubwa@gmail.com>wrote:
Has anyone here successfully (and accurately) determined the size of a C++ class instance (Object) in memory during run time?
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

I might be wrong but clarify this... The problem aki has mentioned sounds like a memory leak and not really the size of the class object. I dont think you can practically write an object that consumes GB's of memory :) Anyway. If I am right and its a memory leak then you have some object thats getting called often and is not being deleted. Eg in a for loop or something. Or an exception of some sort is is causing the break out of a function before the object is deleted. or maybe you can clearly state what exactly the problem is ----- Original Message ----- | From: "James Nzomo" <kazikubwa@gmail.com> | To: "Skunkworks Mailing List" <skunkworks@lists.my.co.ke> | Sent: Monday, November 28, 2011 12:16:21 PM | Subject: Re: [Skunkworks] Size of an object in memory at runtime | Thank you aki, that was a very helpful insight (really). | Are there any C++ 1337s on the list who have encountered this problem | and solved it? | I am trying to avoid painstaking workarounds such as adding up the | size of each member or using JSON. | Anyone? | _______________________________________________ | Good judgement comes from Experience. | Most of that comes from Bad Judgement. | _______________________________________________ | 2011/11/28 aki < aki275@gmail.com > | _______________________________________________ | Skunkworks mailing list | Skunkworks@lists.my.co.ke

@Nzomo, @Obbayi @Handley. The Jedi Council orders your immediate honours a Code Warriors. Yoda still missing words. BTW, this developers thing of turning coders into entrepreneurs looking for investors has really screwed up the coding passions that I believe many have on this list. There should be a year award to recognise Code Warriors. :-)) Seriously, if I'd had someone respond to my query the last time when I asked about Object sizes, I'd surely have moved to C++. Well done, interesting read. Rgds.

@Aki lol _______________________________________________ *Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ * * 2011/11/28 aki <aki275@gmail.com>
@Nzomo, @Obbayi @Handley. The Jedi Council orders your immediate honours a Code Warriors. Yoda still missing words. BTW, this developers thing of turning coders into entrepreneurs looking for investors has really screwed up the coding passions that I believe many have on this list. There should be a year award to recognise Code Warriors. :-))
Seriously, if I'd had someone respond to my query the last time when I asked about Object sizes, I'd surely have moved to C++. Well done, interesting read.
Rgds.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
participants (3)
-
aki
-
James Nzomo
-
Steve Obbayi