473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Memory taken by an object

How do I evaluate, in c#, the amount of memory taken up by any given object
?
Nov 17 '05 #1
6 1439
Hi JezB,

Binary Serialize the object to disk.

Hope that helps,
Jan

Nov 17 '05 #2
int size = System.Runtime.InterpoServices.Marshal.SizeOf(obje ct);

Nov 17 '05 #3
Hi Jan,

Just a note that this way you'll get an estimate and not an exact amount
(due to memory alignment and other context not serialized).
Perhaps the best way is to check it with a memory profiler..

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Jan Bannister (jancsharp.blogspot.com)" <ja***********@gmail.com> wrote in
message news:11**********************@g44g2000cwa.googlegr oups.com...
Hi JezB,

Binary Serialize the object to disk.

Hope that helps,
Jan

Nov 17 '05 #4
> How do I evaluate, in c#, the amount of memory taken up by any
given object?


Neither binary serialization nor marshalling will actually tell you
that information, and there's no way to determine it accurately, in a
guaranteed way.

However, each object has an 8 byte "overhead" plus the space taken for
its member fieds. Each reference is 4 bytes (on a 32 bit CLI), each
Int32 takes 4 bytes, etc. Where it becomes tricky is if you have
byte/short members - depending on attributes, these members may be
"packed" or not.

Don't forget though that two objects could both refer to the same third
object - you'd have to make sure you don't count that third object
twice if you're trying to sum the object sizes...

Jon

Nov 17 '05 #5
Thanks Miha,

Thats handy to know.

Jan

Nov 17 '05 #6
You're welcome :-D

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Jan Bannister (jancsharp.blogspot.com)" <ja***********@gmail.com> wrote in
message news:11**********************@g49g2000cwa.googlegr oups.com...
Thanks Miha,

Thats handy to know.

Jan

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
31
by: William Stacey [MVP] | last post by:
Here is an interesting writing on memory barriers. Not sure if this helps my understanding or raises more questions, but interesting... ...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
3
by: tshad | last post by:
If I create an object (instantiate) then do it again using the same Pointer, will I leave the old object hanging around wasting memory? Would this create a memory leak or would it be taken care...
3
by: Frank Rizzo | last post by:
It is common knowledge that 32-bit processes can use up to 2GB of RAM. This has been my experience with native (vb6, c++) apps. However, .NET apps tend to crash with Out of Memory errors whenever...
17
by: Frank Rizzo | last post by:
Hello, I've compiled my app for AnyCPU setting in vs2005. Then I tried the app on both 32-bit Windows 2003 R2 and 64-bit Windows 2003 R2. The memory usage of the application when working on...
2
by: manojmohadikar2008 | last post by:
Hi All, We are observing a serious issue with the memory usage of Queue and its very critical issue which needs to be fixed. We have an application which runs two threads i.e. a Producer and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.