[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Need to find the size of my object

Greetings,

Is there a SizeOf() function in C# that can tell me the size of an object?  I see quite a few in the help, but I would rather not test them all, so if anyone has experience with one, I'd be grateful.

Please include the namespace as well.

Thanks.

0
ba272
Asked:
ba272
  • 5
  • 3
2 Solutions
 
gregoryyoungCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfsizeofpg.asp works for all value types ... are you wanting to actually walk a full object tree for a reference type ?! this can cause ALOT of issues especially with such things as circular references!
0
 
ba272Author Commented:
Greg,

I was actually just thinking about finding the size of an object.  For example, a DateTime object.  Or an object of my own.


Thanks,

Bob
0
 
gregoryyoungCommented:
There are many problems doing this with object graphs ... for an individual object count its value types/sizes

http://dotnetjunkies.com/WebLog/bikta/articles/17084.aspx

discusses other methods of estimation (binary serialization) but any method is at best a hack ... The reason you can't get this is incase the internal implementations change, providing this information leaves you the possibility of bcoming dependant upon layouts/sizes

Greg
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bob LearnedCommented:
How about System.Runtime.InteropServices.Marshal.SizeOf?

Bob
0
 
gregoryyoungCommented:
its listed in the above example, the problem is that is its marshalled size not its memory size ... object graphs are a real pain to determine the size of :)
0
 
ba272Author Commented:
Greg,

When you say object graphs, are you thinking about objects like DateTime, for example?


Bob
0
 
gregoryyoungCommented:
I am more worried about complex objects (an objects size is determined by its size plus the size of any objects it references)... assume a typed collection foo below ...

public class A {
    public fooCollection B;
}

public class foo {
   public A parent;
}

this for instance would be a real pain to find ... because both objects reference each other thus you would need to remember both in memory as you walked the objects to determine the true size (otherwise you would recurse infinitely). The other problem is often times you end up with one object that has hundreds of thousands of objects it references indirectly (look at a control).

Greg

0
 
ba272Author Commented:
Okay, I understand youir concern.  But how about finding the size of a DateTime object?  Do you have a methodolgy which woud yield a size which you'd have some confidence in?  That particular object is the one which is of most interest to me.

Thanks,
Bob
0
 
gregoryyoungCommented:
I would use a sum of all of its value types (can get easily through reflection) and forget about references (although they do use memory).

Greg
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now