Jump to content
Xtreme .Net Talk

Get the size of an object (amount of memory it takes up).


Recommended Posts

Posted

Is there a way to get the size of an object? C# has the sizeof() keyword, but unfortunately it only works for value types. I need something that'll work for reference types.

 

Meh, posted in wrong forum. Oh well. :)

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

I no of no way to get the size of a managed class.

Here's a blog from Microsoft that talks about this topic though - it might be helpful (I didn't read it through):

http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx

 

Here's an article about memory, might also yield some results though again, I haven't read it:

http://www.albahari.com/value%20vs%20reference%20types.html

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
For vb6, I can hack into memory and get the size by using ObjPtr() and CopyMemory(). But now vb.net doesn't support ObjPtr().
Don't judge a man by his look. Don't judge a book by its cover. :D
  • *Experts*
Posted

You can still pin memory and get a pointer to any managed object BUT to use copymemory you would have to know how big a chunk to copy. I think that's what wyrd is looking for.

 

If it's a simple class, you could try and "guess", but I because of byte alignment I don't know if there's any guarantee that your program won't change on a different platform (when 64bit computers are more normal for instance). Tough call.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

What object is it? If it's yours, why not just use a struct?

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Unfortunately that method only works with an unmanaged object. And if I'm not mistaken, I'd have to allocated more memory in the unmanaged heap, get the size, then deallocate memory from the unmanaged heap, just to get the size (which isn't even accurate since it's the unmanaged size, and not the managed size).

 

It's not what I'm looking for, but thanks anyway.

Gamer extraordinaire. Programmer wannabe.
Posted

What object is it? If it's yours, why not just use a struct?

 

Of any managed object.

Gamer extraordinaire. Programmer wannabe.
  • *Gurus*
Posted
Unfortunately that method only works with an unmanaged object. And if I'm not mistaken, I'd have to allocated more memory in the unmanaged heap, get the size, then deallocate memory from the unmanaged heap, just to get the size (which isn't even accurate since it's the unmanaged size, and not the managed size).

 

It's not what I'm looking for, but thanks anyway.

The method gets the equivalent unmanaged size of any CLR object (useful for platform invocation). It doesn't (and can't) get the size of an unmanaged object. This is the closest you're going to get to what you want.

  • *Experts*
Posted

From my limited digging, you just can't get the size of a managed class - at least through no publicly exposed interface anyone is talking about. Maybe there's a hidden API, but no one's talking.

 

I'm not sure if this will help, but you can get a pointer to any object through the use of a GCHandle object. Check the help on how to use it. Make sure you specify "pinned" memory when you create the handle and then you can safely pass the returned pointer (handle.AddrOfPinnedObject) to an API, if that's what you want/need.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...