The size of the list of each object takes in System.Runtime.Caching cache cache C #

I use the C # memory cache (System.Runtime.Caching) and it fills up and uses the path more than expected. In any case, to indicate the size that each object in the cache occupies in the cache

I want to encode something similar to this so that I can determine which specific element is causing the problem?

private void ListSizeOfEachItemInCache()
{
    foreach {var item in Cache.Items}
    {
        Console.WriteLine(string.Format(item.Key, item.CacheSize));
    }
}

My elements are usually classes that contain collections and have a large graph of objects. However, if I serialize them to disk using a data serializer, they do not seem to fill as much disk space as it seems in RAM. This makes me think of a file cache, it may work better (since items are retrieved from a remote database - not even locally).

private static readonly MemoryCache Cache = MemoryCache.Default;

, , , , , ( , , , , , , ..).

+4
1

, , . , , sizeof. , .

, ,

void Main()
{
    var types = new List<object>{
        (byte)1,
        (short)1,
        (int)1,
        (long)1,
        (double)1,
        (decimal)1,
        (float)1,
        "omg afd obj amd",
        "omg afd obj amd omg afd obj amd omg afd obj amd omg afd obj amd omg afd obj amd omg afd obj amd omg afd obj amd ",
        new Exception("Holy Maceral"),
    };

    foreach(var o in types)
    {
        Console.WriteLine("Size of {0} == {1}",o.GetType().Name,o.SizeOf());
    }
}
public static class ExtensionMethods
{
    public static int SizeOf(this object obj)
    {
        int size = 0;
        if(obj == null)
            return 0;
        Type type = obj.GetType();

        if(type.IsValueType)
        {
            return type.SizeOfType();
        }

        PropertyInfo[] info = type.GetProperties();
        foreach(PropertyInfo property in info)
        {
            if(property.GetIndexParameters().Length > 0)
            {
                var ip = property.GetIndexParameters().First();
                //Console.WriteLine(info);
                var len = (int)info.FirstOrDefault(x=>x.Name == "Length" || x.Name == "Count").GetValue(obj);
                for(var i = 0;i<len;i++)
                {
                    size += property.GetValue(obj,new object[1]{i}).SizeOf();
                }
            }
            else if(property.GetGetMethod().ReturnType.IsArray)
            {
                var arr = property.GetGetMethod().Invoke(obj,null);
                foreach(var a in (Array)arr)
                {
                    size+= a.SizeOf();
                }
            }
            else if(property.PropertyType.IsValueType)
            {
                var val = property.GetValue(obj,null);
                unsafe
                {
                    size += property.PropertyType.SizeOfType();
                }
            }
            else{
                size += property.GetValue(obj,null).SizeOf();
            }
        }
        return size;
    }

    public static int SizeOfType(this Type type)
    {
        if(type.IsValueType == false)
            throw new ArgumentException("type must be value type");

        return Marshal.SizeOf(type);
    }
}

Size of Byte == 1
Size of Int16 == 2
Size of Int32 == 4
Size of Int64 == 8
Size of Double == 8
Size of Decimal == 16
Size of Single == 4
Size of String == 19
Size of String == 116
Size of Exception == 36

100% - , , , .

0

Source: https://habr.com/ru/post/1530236/


All Articles