public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            if (_isLowMemory.Raise())
            {
                Interlocked.Increment(ref _generation);
            }

            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            if (_isExtremelyLowMemory.Raise() == false)
            {
                return;
            }

            for (int i = 0; i < _items.Length; i++)
            {
                ClearStack(_globalStacks[i]);

                foreach (var allocation in _items[i].EnumerateAndClear())
                {
                    if (allocation.InUse.Raise())
                    {
                        allocation.Dispose();
                    }
                }
            }
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            if (_isLowMemory.Raise())
            {
                Interlocked.Increment(ref _generation);
            }

            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            if (_isExtremelyLowMemory.Raise() == false)
            {
                return;
            }

            foreach (var container in _items)
            {
                foreach (var allocation in container.EnumerateAndClear())
                {
                    if (allocation.InUse.Raise())
                    {
                        allocation.Dispose();
                    }
                }
            }
        }
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            if (_isLowMemory.Raise())
            {
                Interlocked.Increment(ref _generation);
            }

            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            if (_isExtremelyLowMemory.Raise() == false)
            {
                return;
            }

            foreach (var container in _items)
            {
                while (container.TryPull(out var allocation))
                {
                    if (allocation.InUse.Raise() == false)
                    {
                        continue;
                    }

                    allocation.Dispose();
                }
            }
        }
 private void RunLowMemoryHandlers(bool isLowMemory, LowMemorySeverity lowMemorySeverity = LowMemorySeverity.ExtremelyLow)
 {
     try
     {
         foreach (var lowMemoryHandler in _lowMemoryHandlers)
         {
             if (lowMemoryHandler.TryGetTarget(out var handler))
             {
                 try
                 {
                     if (isLowMemory)
                     {
                         handler.LowMemory(lowMemorySeverity);
                     }
                     else if (_wasLowMemory)
                     {
                         handler.LowMemoryOver();
                     }
                 }
                 catch (Exception e)
                 {
                     try
                     {
                         if (_logger.IsInfoEnabled)
                         {
                             _logger.Info("Failure to process low memory notification (low memory handler - " + handler + ")", e);
                         }
                     }
                     catch
                     {
                         // ignored
                     }
                 }
             }
             else
             {
                 // make sure that we aren't allocating here, we reserve 128 items
                 // and worst case we'll get it in the next run
                 if (_inactiveHandlers.Count < _inactiveHandlers.Capacity)
                 {
                     _inactiveHandlers.Add(lowMemoryHandler);
                 }
             }
         }
         foreach (var x in _inactiveHandlers)
         {
             if (x == null)
             {
                 continue;
             }
             _lowMemoryHandlers.TryRemove(x);
         }
     }
     finally
     {
         _wasLowMemory = isLowMemory;
         _inactiveHandlers.Clear();
     }
 }
Beispiel #5
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
     {
         return;
     }
     ReleaseUnmanagedResources();
 }
Beispiel #6
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
     {
         return;
     }
     FreeSpace();
 }
Beispiel #7
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     InLowMemoryMode.Raise();
     if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
     {
         return;
     }
     // can't call clear, etc, just forget the whole thing, so GC will clean
     CacheByReader = new();
 }
Beispiel #8
0
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            if (_lowMemoryFlag.Raise())
            {
                Clear();
            }
        }
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            foreach (var state in _subscriptionConnectionStates)
            {
                if (state.Value.Connection != null)
                {
                    continue;
                }

                _subscriptionConnectionStates.Remove(state.Key, out _);
            }
        }
Beispiel #10
0
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            var size = FreeAllPooledMemory();

            if (_log.IsInfoEnabled)
            {
                _log.Info($"{_debugTag}: HandleLowMemory freed {new Size(size, SizeUnit.Bytes)} in {_debugTag}");
            }
        }
Beispiel #11
0
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            var size = FreeAllPooledMemory();

            if (_log.IsInfoEnabled)
            {
                _log.Info($"{_debugTag}: HandleLowMemory freed {size:#,#;;0} bytes in {_debugTag}");
            }
        }
Beispiel #12
0
        public void clear_all_buffers_from_current_generation_on_low_memory(LowMemorySeverity lowMemorySeverity)
        {
            var encryptionBuffersPool = new EncryptionBuffersPool();
            var generation            = encryptionBuffersPool.Generation;
            var i      = 1;
            var toFree = new List <(IntPtr, long)>();

            while (i <= new Size(8, SizeUnit.Megabytes).GetValue(SizeUnit.Bytes))
            {
                var ptr = encryptionBuffersPool.Get(i, out _);
                toFree.Add(((IntPtr)ptr, i));

                i *= 2;
            }

            var stats = encryptionBuffersPool.GetStats();

            Assert.Equal(0, stats.TotalSize);

            encryptionBuffersPool.LowMemory(lowMemorySeverity);
            stats = encryptionBuffersPool.GetStats();
            Assert.Equal(0, stats.TotalSize);

            foreach (var o in toFree)
            {
                encryptionBuffersPool.Return((byte *)o.Item1, o.Item2, NativeMemory.ThreadAllocations.Value, generation);
            }

            stats = encryptionBuffersPool.GetStats();
            Assert.Equal(0, stats.TotalSize);

            var size    = 8 * 1024;
            var pointer = encryptionBuffersPool.Get(size, out _);

            encryptionBuffersPool.Return(pointer, size, NativeMemory.ThreadAllocations.Value, encryptionBuffersPool.Generation);

            // will cache the buffer
            stats = encryptionBuffersPool.GetStats();
            Assert.Equal(size, stats.TotalSize);

            // will continue to cache the buffer
            encryptionBuffersPool.LowMemory(lowMemorySeverity);
            stats = encryptionBuffersPool.GetStats();
            Assert.Equal(size, stats.TotalSize);

            encryptionBuffersPool.LowMemoryOver();
            ClearMemory(encryptionBuffersPool);
        }
        public void LowMemory(LowMemorySeverity lowMemorySeverity)
        {
            var alreadyInLowMem = !LowMemoryFlag.Raise();

            if (lowMemorySeverity != LowMemorySeverity.ExtremelyLow)
            {
                return;
            }

            if (alreadyInLowMem)
            {
                return;
            }

            Interlocked.Increment(ref _generation);

            while (_globalQueue.TryDequeue(out var result))
            {
                if (result.InUse.Raise())
                {
                    result.Dispose();
                }
            }

            foreach (var coreContext in _perCoreContexts)
            {
                for (int i = 0; i < coreContext.Length; i++)
                {
                    var context = coreContext[i];
                    if (context != null && context.InUse.Raise())
                    {
                        context.Dispose();
                        Interlocked.CompareExchange(ref coreContext[i], null, context);
                    }
                }
            }
        }
Beispiel #14
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     // cleanup operations older than 1 minute only
     // Client API might still be waiting for the status
     CleanupOperationsInternal(_completed, TimeSpan.FromMinutes(1));
 }
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     _isLowMemory = true;
 }
Beispiel #16
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     RunIdleOperations();
 }
Beispiel #17
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     TermsCachePerReader.Clear();
 }
Beispiel #18
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     Results.Clear();
 }
Beispiel #19
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     _cache.Clear();
 }
Beispiel #20
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     // will defer the timeout event if we detected low-memory
     _timeoutEventSlim.Set();
 }
Beispiel #21
0
 public void LowMemory(LowMemorySeverity lowMemorySeverity)
 {
     _lastLowMemoryEventTicks = DateTime.UtcNow.Ticks;
     _lowMemoryFlag.Raise();
 }