AnointedAutomation.Memory
2.0.0
dotnet add package AnointedAutomation.Memory --version 2.0.0
NuGet\Install-Package AnointedAutomation.Memory -Version 2.0.0
<PackageReference Include="AnointedAutomation.Memory" Version="2.0.0" />
<PackageVersion Include="AnointedAutomation.Memory" Version="2.0.0" />
<PackageReference Include="AnointedAutomation.Memory" />
paket add AnointedAutomation.Memory --version 2.0.0
#r "nuget: AnointedAutomation.Memory, 2.0.0"
#:package AnointedAutomation.Memory@2.0.0
#addin nuget:?package=AnointedAutomation.Memory&version=2.0.0
#tool nuget:?package=AnointedAutomation.Memory&version=2.0.0
AnointedAutomation.Memory
Jesus is King ✝️
A small .NET utility for on-demand garbage collection. It forces a full collection, waits for finalizers, and then tries to hand unused memory back to the operating system, which is useful for long running services after a memory heavy job.
Installation
dotnet add package AnointedAutomation.Memory
- Target framework:
net10.0 - Dependencies:
AnointedAutomation.Logging(a refused no-GC region is logged as a Warning)
Note that the namespace is AnointedAutomation.Optimization.Memory, not the package id.
Quick start
using AnointedAutomation.Optimization.Memory;
GarbageCollection gc = new GarbageCollection();
// After a large import, export, or batch job:
gc.PerformGarbageCollection(null);
API overview
Namespace: AnointedAutomation.Optimization.Memory
| Member | Description |
|---|---|
GarbageCollection() |
Creates an instance. The class holds no state. |
void PerformGarbageCollection(object state) |
Runs GC.Collect(), then GC.WaitForPendingFinalizers(), then attempts to return unused memory to the OS. state is unused. |
How the memory return works:
- Reads the current managed heap size with
GC.GetTotalMemory(false). - If it is greater than zero, calls
GC.TryStartNoGCRegionwith that size. - If the region starts, immediately calls
GC.EndNoGCRegion(). - An
InvalidOperationExceptionfrom this step is swallowed, so the call never fails on it.
Running on a timer
PerformGarbageCollection takes an object state parameter so it matches the TimerCallback delegate and can be scheduled directly:
using System;
using System.Threading;
using AnointedAutomation.Optimization.Memory;
GarbageCollection gc = new GarbageCollection();
Timer timer = new Timer(gc.PerformGarbageCollection, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(30));
Keep a reference to the Timer for as long as it should run.
Best practices
- Use sparingly. A forced full collection pauses the application and usually is not needed; the .NET GC tunes itself well.
- Best suited to the moment right after a large, short lived allocation spike.
- Measure before and after (for example with
GC.GetTotalMemoryordotnet-counters) to confirm it helps your workload.
Nullable reference types (2.0.0)
The package is compiled with nullable reference types enabled. GarbageCollection.PerformGarbageCollection(object? state) now declares its unused timer state as nullable (a TimerCallback passes null). The change is source compatible: passing null, or a possibly null value, produces no warning, so no consumer sees a new warning.
Related packages
- AnointedAutomation.APIMiddlewares: ASP.NET Core middlewares that depend on this package.
License
MIT. See LICENSE. Copyright © Anointed Automation Corporation. Stewarded by Alexander Fields.
Support This Project
This library is free and open source. The best way to support the work is to shop with us:
- Christian items: https://store.anointed.company
- Pets: https://www.animaldander.com
- Everything else: https://www.mart.club
Found a bug or have a request? Open an issue: https://github.com/AnointedAutomation/AnointedAutomation/issues
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AnointedAutomation.Logging (>= 3.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on AnointedAutomation.Memory:
| Package | Downloads |
|---|---|
|
AnointedAutomation.APIMiddlewares
ASP.NET Core middleware and filters for API key authentication, IP blacklisting, country blocking, honeypot and bot defense (nothing is on by default; see the README), invalid endpoint tracking and idle time memory cleanup. |
GitHub repositories
This package is not used by any popular GitHub repositories.