Beispiel #1
0
        protected override int GetCurrentPressure()
        {
            int  num  = GC.CollectionCount(2);
            SRef ref2 = this._sizedRef;

            if ((num != this._gen2Count) && (ref2 != null))
            {
                this._gen2Count = num;
                this._idx      ^= 1;
                this._cacheSizeSampleTimes[this._idx] = DateTime.UtcNow;
                this._cacheSizeSamples[this._idx]     = ref2.ApproximateSize;
                IMemoryCacheManager manager = s_memoryCacheManager;
                if (manager != null)
                {
                    manager.UpdateCacheSize(this._cacheSizeSamples[this._idx], this._memoryCache);
                }
            }
            if (this._memoryLimit <= 0L)
            {
                return(0);
            }
            long num2 = this._cacheSizeSamples[this._idx];

            if (num2 > this._memoryLimit)
            {
                num2 = this._memoryLimit;
            }
            return((int)((num2 * 100L) / this._memoryLimit));
        }
        protected override int GetCurrentPressure()
        {
            // Call GetUpdatedTotalCacheSize to update the total
            // cache size, if there has been a recent Gen 2 Collection.
            // This update must happen, otherwise the CacheManager won't
            // know the total cache size.
            int          gen2Count = GC.CollectionCount(2);
            SRefMultiple sref      = _sizedRefMultiple;

            if (gen2Count != _gen2Count && sref != null)
            {
                // update _gen2Count
                _gen2Count = gen2Count;

                // the SizedRef is only updated after a Gen2 Collection

                // increment the index (it's either 1 or 0)
                Dbg.Assert(SAMPLE_COUNT == 2);
                _idx = _idx ^ 1;
                // remember the sample time
                _cacheSizeSampleTimes[_idx] = DateTime.UtcNow;
                // remember the sample value
                _cacheSizeSamples[_idx] = sref.ApproximateSize;
#if DBG
                Dbg.Trace("MemoryCacheStats", "SizedRef.ApproximateSize=" + _cacheSizeSamples[_idx]);
#endif
                IMemoryCacheManager memoryCacheManager = s_memoryCacheManager;
                if (memoryCacheManager != null)
                {
                    memoryCacheManager.UpdateCacheSize(_cacheSizeSamples[_idx], _memoryCache);
                }
            }

            // if there's no memory limit, then there's nothing more to do
            if (_memoryLimit <= 0)
            {
                return(0);
            }

            long cacheSize = _cacheSizeSamples[_idx];

            // use _memoryLimit as an upper bound so that pressure is a percentage (between 0 and 100, inclusive).
            if (cacheSize > _memoryLimit)
            {
                cacheSize = _memoryLimit;
            }

            // PerfCounter: Cache Percentage Process Memory Limit Used
            //    = memory used by this process / process memory limit at pressureHigh
            // Set private bytes used in kilobytes because the counter is a DWORD

            //


            int result = (int)(cacheSize * 100 / _memoryLimit);
            return(result);
        }