AnointedAutomation.Memory
1.0.6
dotnet add package AnointedAutomation.Memory --version 1.0.6
NuGet\Install-Package AnointedAutomation.Memory -Version 1.0.6
<PackageReference Include="AnointedAutomation.Memory" Version="1.0.6" />
<PackageVersion Include="AnointedAutomation.Memory" Version="1.0.6" />
<PackageReference Include="AnointedAutomation.Memory" />
paket add AnointedAutomation.Memory --version 1.0.6
#r "nuget: AnointedAutomation.Memory, 1.0.6"
#:package AnointedAutomation.Memory@1.0.6
#addin nuget:?package=AnointedAutomation.Memory&version=1.0.6
#tool nuget:?package=AnointedAutomation.Memory&version=1.0.6
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: none
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.
Related packages
- AnointedAutomation.APIMiddlewares: ASP.NET Core middlewares that depend on this package.
License
MIT. See LICENSE. Copyright © Anointed Automation, LLC. 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
- No dependencies.
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, invalid endpoint tracking and idle time memory cleanup. |
GitHub repositories
This package is not used by any popular GitHub repositories.