1

One of my legacy application heavily used MemoryCache, therefore I was trying to know how memory cache internally works but now I'm totally confused after some below experiments. Please help me to resolve my confusion.

Question 1 how to check system cache memory? I used below command on command prompt but not able to understand result

wmic cpu get L2CacheSize, L2CacheSpeed, L3CacheSize, L3CacheSpeed

Result

L2CacheSize  L2CacheSpeed  L3CacheSize  L3CacheSpeed
    512                        4096         0

Question 2 I used C# code to check cache memory size and available cache memory size, therefore I write below code. why program 2 not throwing error? & how to check available cache memory size? (program 3 throwing error)

Program 1:

var memeoryCache = new MemoryCache("CacheItems");
            Console.WriteLine(memeoryCache.CacheMemoryLimit);
            Console.WriteLine(memeoryCache.PhysicalMemoryLimit);

Result

838860800//Get amount of memory on the computer, in byte, that can be used by cache
99//Gets the percentage of physical memory that the cache can use.

Program2:

var memeoryCache = new MemoryCache("CacheItems");
            Console.WriteLine("***** Memory Cache Test *****");
            Console.WriteLine(memeoryCache.CacheMemoryLimit);
            Console.WriteLine(memeoryCache.PhysicalMemoryLimit);
            var bytearray = new byte[838860800];
            memeoryCache.Add("Test", bytearray, DateTimeOffset.Now.AddMinutes(10));
            var bytearray2 = new byte[100000];
            memeoryCache.Add("Test2", bytearray2, DateTimeOffset.Now.AddMinutes(10));
            Console.ReadLine();
            var test = memeoryCache.Get("Test");
            var test2 = memeoryCache.Get("Test2");
            Console.ReadLine();

Progma3:

var memeoryCache = new MemoryCache("CacheItems");
            Console.WriteLine("***** Memory Cache Test *****");
            Console.WriteLine(memeoryCache.CacheMemoryLimit);
            Console.WriteLine(memeoryCache.PhysicalMemoryLimit);
            var bytearray = new byte[838860800];
            memeoryCache.Add("Test", bytearray, DateTimeOffset.Now.AddMinutes(10));
            var bytearray2 = new byte[838860800];
            memeoryCache.Add("Test2", bytearray2, DateTimeOffset.Now.AddMinutes(10));
            Console.ReadLine();
            var test = memeoryCache.Get("Test");
            var test2 = memeoryCache.Get("Test2");
            Console.ReadLine();

Result:

An unhandled exception of type 'System.OutOfMemoryException' occurred in MemoryCacheTest.exe

Question 3 How to check what is stored in cache memory? or Which application data stored in cache memory?

My Task manager

Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73
  • You should ask one question at a time. Second - dup of http://stackoverflow.com/questions/6895956/memorycache-does-not-obey-memory-limits-in-configuration?rq=1, third likely not realted to MemoryCache - [MCVE] should show problem with just 2 array allocations... – Alexei Levenkov Jan 22 '17 at 05:49
  • Logically I have one question but tried to explain that is why I break down my queries. Why 90 min limitation for second question ? "You can only post once every 90 minutes." – Pankaj Rawat Jan 22 '17 at 06:03
  • One reason for restriction is to give users some time to do research of the questions to [show they did expected amount of research](http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). I.e. one can notice that to see what items are in the cache one can check if `MemoryCache` is `IEnumerable` and see if they can look at items that way - than ask question with details about what they found and how it did not solve concrete problem they have. – Alexei Levenkov Jan 22 '17 at 06:18
  • "Cache" is a generic name for any memory used to store results that are expensive to reproduce. `MemoryCache` is an implementation of a cache. So are the caches on your CPU (the result you get with `wmic`). So is the operating system cache (the result you get from Task Manager). *None of these caches are the same cache and it makes no sense to compare them directly*. What is your *real* question, as in, what *problem* are you trying to solve? Does your app run out of memory? Explaining how every caching level everywhere works is far too broad a topic for a question. – Jeroen Mostert Jan 23 '17 at 08:36
  • @JeroenMostert my application running fine, I just want to understand memeoryCache deeply. Q1, how much data we can store in cache and how we can check that in C#? Q2. How system release cache resource from memory if cache size exceed? I have checked there is no FIFO or LIFO logic. Q3 why cache use shallow cloning instead of deep cloning? – Pankaj Rawat Jan 23 '17 at 10:36
  • You'll find that Stack Overflow isn't a good site for answering broad "tell me how X works" questions like this, and comments are an even worse medium. Except for Q3 above, your questions can be answered from the [documentation](https://msdn.microsoft.com/library/system.runtime.caching.memorycache.memorycache) and the [source](https://referencesource.microsoft.com/#System.Runtime.Caching/System/Caching/MemoryCacheStore.cs,83671340f41de22b), but as SO questions you'd have to do one at a time, as tedious as that sounds. And note that "I don't understand" isn't a question. – Jeroen Mostert Jan 23 '17 at 11:06
  • You have found several uses of the word Cache, none of them relate to the MemoryCache or its size. – H H Jan 23 '17 at 14:08

0 Answers0