0

I am using the memory cache reserve data in memory about 120 GB I know its too large but its the best practice for my project the issue is I am using the memory cache object as the following

I created static object for the cache memory

var collection = new System.Collections.Specialized.NameValueCollection();
            collection.Add("CacheMemoryLimitMegaBytes", "155000");
            //var memoryCashe = new MemoryCache("Images", collection);
            MemoryCachingProvider.cache = new MemoryCache("Images", collection);
//Here how I add new item

CacheItemPolicy policy = new CacheItemPolicy();
                    policy.Priority = CacheItemPriority.NotRemovable;
                    cache.Add(key, value, policy);

but the memory became empty after period of time if I didn't use it.

I am building web API application

and I have enough space in the RAM about 170 GB

any suggestions

5
  • 1
    It is "cacheMemoryLimitMegaBytes", lowercase c. Commented Sep 25, 2017 at 14:24
  • could this cause this issue no error raised when i used it like this Commented Sep 25, 2017 at 14:34
  • 1
    No need to ask when you can simply try it. Type "asdf". Commented Sep 25, 2017 at 14:35
  • You're trying to use the cache as permanent storage. That will create all kinds of issues. Commented Sep 25, 2017 at 15:00
  • No i just need to be in memory for some hours not permanent Commented Sep 25, 2017 at 17:58

3 Answers 3

2

As mentioned by others, this is probably happening because of the IIS server Recycling. "Turning off" the recycle can be a turnaround solution to your problem and will work as an emergency solution. But if I were in your pants, after applying this turnaround, I would try different ways to use the memory cache without disabling the recycling process.

Recycling

Recycling is usually* where IIS starts up a new process as a container for your application, and then gives the old one up to ShutdownTimeLimit to go away of its own volition before it's killed.

*- usually: see DisallowOverlappingRotation / "Disable overlapped recycle" setting

It is destructive, in that the original process and all its state information are discarded (including your precious memory cache).

What To Do:

Generally:

  • Disable Idle timeouts. 20 minutes of inactivity = boom! New process on the next incoming request. Set that to zero.
  • Disable Regular time interval - the "29 hour" default has been described as "insane", "annoying" and "clever" by various parties. Actually, only two of those are true.
  • Optionally Turn on DisallowRotationOnConfigChange (above, Disable Recycling for configuration changes) if you just can't stop playing with it - this allows you to change any app pool setting without it instantly signaling to the worker processes that it needs to be killed. You need to manually recycle the App Pool to get the settings to take effect, which lets you pre-set settings and then use a change window to apply them via your recycle process.

That's enough to cause a well-behaved process to live forever. If it dies, sure, it'll be replaced. If it hangs, pinging should pick that up and a new one should start within 2 minutes (by default; worst-case calc should be: up to ping frequency + ping timeout + startup time limit before requests start working again).

Sugestion:

Using out-of-process session state (eg, State Server or a database, or even a cookie if your state is tiny) can allow you to work around this without disabling the recycle.


Hope it helps!

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

Comments

1

Caches are temporary by nature. If IIS spins down the app pool, then all such resources will get recycled. You will need to ensure the app pool never recycles (almost impossible) or need to move this cache to an external, durable resource such as redis.

4 Comments

I there Any way to reserve the memory also I checked the app pool settings its not configured to recycle, I used redis but not met my requirements I need all data one shot so the traffic will limit the speed
Various other timeouts can also spin down the app pool. Also, if there's a hang, IIS can independently kill it regardless of configuration so that it can continue to serve requests to the site it is assigned to. Your issue is the length of time you want the data to live not so much where. When you say you need all data one shot, what do you mean?
I need the data for some hours as long as possible, when i said one shot I mean I need all data together not getting each item alone I will process the whole data, And I need the available in application level shared between all sessions
Nothing you've described so far excludes some external product ensuring your cache is reliable.
0

Usually if it's "after period of time", it means you probably have a SlidingExpiration value other than "NoSlidingExpiration".

Other than that, it could be that your application pool is recycling at specific interval, and that gives you the impression that the cache is "emptied".

1 Comment

No sliding expiration and I checked the app pool not configured to recycle what will be the reason

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.