programmatically find memory used by object

后端 未结 2 1153
一向
一向 2021-02-08 22:38

Is there a way to programmatically and accurately determine the amount of memory used by an object in c#? I am not concerned with how slow the process is, so running GCs left a

2条回答
  •  無奈伤痛
    2021-02-08 23:07

    object obj = new List(); // whatever you want to get the size of
    RuntimeTypeHandle th = obj.GetType().TypeHandle;
    int size = *(*(int**)&th + 1);
    Console.WriteLine(size);
    

    I dont know whether it is useful to you or not...but try to refer this link...espacialy fig.4

    http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S9

提交回复
热议问题