The current time is

Monday, May 26, 2008

Determining the size of .Net Managed objects residing in memory

Lately I've been checking how to test for memory performance issues and I was hit by Mark Brown's blog... Liked his snippet alot although it's not thread safe but still it can help...
Here's the snippet:

long StopBytes = 0;
foo myFoo;

long StartBytes = System.GC.GetTotalMemory(true);
myFoo = new foo();
StopBytes = System.GC.GetTotalMemory(true);
GC.KeepAlive(myFoo); // This ensures a reference to the object kept in memory


MessageBox.Show("Size is " + ((long)(StopBytes - StartBytes)).ToString());

However, I guess it would be better if one would get the StopBytes after the statement of KeepAlive... Don't you think?

No comments:

Post a Comment