There is some articles about How to get object size in memory ? but they does not explain how to get the size of an object in memory.
when I use:
System.Runtime.InteropServices.Marshal.SizeOf(arrayListObject)
i get error:
Type 'System.Collections.ArrayList' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
I开发者_高级运维 also can not get the amount of all free memory, becouse i want to perform this calculation on web applications with a lot of threads, so a need to know exactly how much memory needs specific object.
You want to find out the size in memory of managed objects in code - according to this blog entry, it is not possible.
You need to use a memory profiler to do this (like the Ants profiler).
If you want to know this because of possible optimizations: Use a memory profiler.
And what about serializing the DataTable, and then checking its length?
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
formatter.Serialize(stream, YourDataTable);
long length = stream.Length;
精彩评论