1

Lets say I have object Tom which has class Person.

Class Person

String Name
DateTime BirthDate
String Role
Int32 Salary

Can you give me an approximation of its memory size if it has the following values:

Name = Tom. BirthDate = 1/1/1990. Role = User. Salary = 30000

Could you also give me some insight into how the calculation was done?

Tomasi
  • 2,517
  • 6
  • 31
  • 43

1 Answers1

2

To get a true figure, use the CLR profiler. To do it programmatically, use sizeof (only works on value types). You could also do it by referencing the GC directly like this...

long StopBytes = 0;
foo myFoo;

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

long TotalBytes = StopBytes - StartBytes;
MessageBox.Show("Size is " + TotalBytes.ToString());
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227