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.

2
  • 2
    Unless you're still stuck at .NET 1.1, you should not use ArrayList. Use List<object> if you need a list containing any object. Commented Mar 4, 2010 at 12:03
  • Question is not about the way to use objects, For now I'm working with DataTable Commented Mar 9, 2010 at 5:19

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I dont want to use profiler, i need to know the size to fire an alert/event or something
1

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

1 Comment

no, i don't want to know it for optimization, I want to know when my DataTable grows to much, to fire an event...
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.