3

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.

Community
  • 1
  • 1
Alexei.Ozon
  • 55
  • 1
  • 7

3 Answers3

2

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).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

If you want to know this because of possible optimizations: Use a memory profiler.

Matthias
  • 12,053
  • 4
  • 49
  • 91
  • no, i don't want to know it for optimization, I want to know when my DataTable grows to much, to fire an event... – Alexei.Ozon Mar 09 '10 at 05:20
0

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;
thepirat000
  • 12,362
  • 4
  • 46
  • 72