Thanks Barry for your informative input.
Hi Praveen,
Thank you for your post.
As Barry pointed out, a good expiration policy of is key to your caching
system.
To get memory size of a single type, we can use Type.TypeHandle to get the
basic instance size of this type. You can use following code to test:
=====
using System;
class SimpleClass
{
private byte b1 = 1; // 1 byte
private byte b2 = 2; // 1 byte
private byte b3 = 3; // 1 byte
private byte b4 = 4; // 1 byte
private char c1 = 'A'; // 2 bytes
private char c2 = 'B'; // 2 bytes
private short s1 = 11; // 2 bytes
private short s2 = 12; // 2 bytes
private int i1 = 21; // 4 bytes
private long l1 = 31; // 8 bytes
private string str = "MyString"; // 4 bytes (only OBJECTREF)
//Total instance variable size = 28 bytes
static void Main()
{
SimpleClass simpleObj = new SimpleClass();
int basicInstanceSize = GetBasicInstanceSize(simpleObj.GetType());
Console.Write(basicInstanceSize);
}
/// <summary>
/// Gets the size of instances of the given type. It is the sum of:
/// - a fixed object overhead (8 bytes)
/// - the sizes of all the instance fields (note: may include padding)
/// For more information see:
/// -
http://msdn.microsoft.com/msdnmag/issues/05/05/JITCompiler/default.aspx
/// -
http://msdn.microsoft.com/msdnmag/issues/05/05/JITCompiler/default.aspx?fig=
true#fig9
/// </summary>
/// <param name="type">A type.</param>
/// <returns>Size in bytes of instances of the given type.</returns>
private static int GetBasicInstanceSize(Type type)
{
// Read the first 8 bytes (=2 ints) of the MethodTable. The basic
instance size is the second int.
IntPtr methodTableIntPtr = IntPtr.Zero;
try
{
methodTableIntPtr = type.TypeHandle.Value;
int[] methodTableData = new int[2];
System.Runtime.InteropServices.Marshal.Copy(methodTableIntPtr,
methodTableData, 0, 2);
int basicInstanceSize = methodTableData[1];
return basicInstanceSize;
}
finally
{
if (methodTableIntPtr != IntPtr.Zero)
System.Runtime.InteropServices.Marshal.Release(methodTableIntPtr);
}
}
}
=====
Hope this helps. Please feel free to post here if anything is unclear.
Sincerely,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.