Token导航 LogoToken导航

Agent Skill · 开发

dotnet-gc-memory(dotnet GC 内存)

dotnet-gc-memory 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

dotnet-gc-memory 是开发类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为需要联网;许可证信息还不明确,第三方扫描有未通过项,按安装量在开发类的 21,746 个 Skill 里排第 17,122。

总安装 441周安装 18GitHub Stars 15下载 141核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
unknown

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:dotnet-gc-memory(dotnet GC 内存)
来源仓库:https://github.com/wshaddix/dotnet-skills
仓库路径:skills/dotnet-gc-memory
安装命令:
npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-gc-memory
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-gc-memory

这个 Skill 是做什么的

开发类 Skill 面向写代码、改代码和排查问题,一般会读写仓库文件,部分还会执行构建或测试命令。

该技能提供 .NET 垃圾回收和内存管理的深度优化指导,降低 GC 压力。适用于高并发、大对象和内存敏感型应用场景的性能调优。核心能力包括 LOH/POH 管理、Span 所有权控制和内存池使用。使用时应结合性能剖析工具验证调整效果。

dotnet-gc-memory 属于开发类 Skill,可作为该场景下的辅助能力补充。安装前需确认项目已启用诊断端点和性能计数器支持。

来源仓库内路径:skills/dotnet-gc-memory

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-gc-memor…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    它需要需要联网,先在测试目录或测试账号里跑,确认没问题再放到正式项目。

常见问题

6 条

dotnet-gc-memory 怎么安装?

在终端执行 npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-gc-memory。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

dotnet-gc-memory 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

dotnet-gc-memory 需要什么权限?

权限级别是需要联网。该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

dotnet-gc-memory 是免费的吗?

公开资料没有标注明确许可证,收费和商用限制都无法确认。正式使用前请到来源仓库核对授权说明。

dotnet-gc-memory 用的人多吗?

公开数据是累计安装 441 次,近一周安装 18 次,下载 141 次,GitHub 15 星。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

dotnet-gc-memory 安全吗?

第三方扫描有未通过项,风险较高。除非你确认过源码,否则不建议直接安装到常用环境。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。

先想清楚的地方

  • 许可证没有明确标注,能不能商用、能不能再分发都不确定。
  • 第三方扫描存在提醒项或未通过项,需要人工逐条确认。
  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理开发类任务、又不想每次都手写提示词的人。

不太适合

  • 需要明确授权才能用到公司项目里的场景。
  • 需要厂商级技术支持和 SLA 的生产系统。

开发类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
skill-vetter

技能审查员 · OpenClaw

8,779,018986操作浏览器MIT-0通过
ontology

本体论 · OpenClaw

6,636,960576只读MIT-0通过
entra-app-registration

内部应用程序注册 · Codex、Claude

6,631,872764接触敏感数据未明确通过
entra-app-registration

内部应用程序注册 · Codex、Claude

3,757,680201接触敏感数据未明确通过
nano-banana-pro

纳米香蕉专业版 · OpenClaw

3,526,898364接触敏感数据MIT-0有提醒
dotnet-gc-memory

dotnet GC 内存 · 本页

44115需要联网未明确未通过

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

dotnet-gc-memory

Garbage collection and memory management for.NET applications. Covers GC modes (workstation vs server, concurrent vs non-concurrent), Large Object Heap (LOH) and Pinned Object Heap (POH), generational tuning (Gen0/1/2), memory pressure notifications, deep Span/Memory ownership patterns beyond basics, buffer pooling with ArrayPool and MemoryPool, weak references, finalizers vs IDisposable, and memory profiling with dotMemory and PerfView.

Out of scope: Span/Memory syntax introduction and basic usage -- see [skill:dotnet-performance-patterns]. Microbenchmarking setup -- see [skill:dotnet-benchmarkdotnet]. CLI diagnostic tools (dotnet-counters, dotnet-trace, dotnet-dump) -- see [skill:dotnet-profiling]. Channel producer/consumer patterns -- see [skill:dotnet-channels].

Cross-references: [skill:dotnet-performance-patterns] for Span/Memory basics and sealed devirtualization, [skill:dotnet-profiling] for runtime diagnostic tools (dotnet-counters, dotnet-trace, dotnet-dump), [skill:dotnet-channels] for backpressure patterns that interact with memory management, [skill:dotnet-file-io] for MemoryMappedFile usage and POH buffer patterns in file I/O.


GC Modes and Configuration

Workstation vs Server GC

AspectWorkstationServer
GC threadsSingle threadOne thread per logical core
Heap segmentsSingle heapOne heap per core
Pause latencyLowerHigher (more memory scanned)
ThroughputLowerHigher
Default forConsole apps, desktopASP.NET Core web apps
<!-- In the .csproj file -->
<PropertyGroup>
  <ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>
// Or in runtimeconfig.json
{
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  }
}

Concurrent vs Non-Concurrent GC

ModeBehaviorUse when
Concurrent (default)Gen2 collection runs alongside application threadsLatency-sensitive (web APIs, UI)
Non-concurrentApplication threads pause during Gen2 collectionMaximum throughput, batch processing
{
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Concurrent": true
    }
  }
}

DATAS (Dynamic Adaptation to Application Sizes) --.NET 8+

DATAS dynamically adjusts GC heap size based on application memory usage patterns. It is enabled by default in.NET 8+ Server GC mode. DATAS reduces memory footprint for applications with variable load by shrinking the heap during low-activity periods.

{
  "runtimeOptions": {
    "configProperties": {
      "System.GC.DynamicAdaptationMode": 1
    }
  }
}

Set to 0 to disable DATAS if you observe excessive GC frequency in steady-state workloads.

GC Regions --.NET 7+

Regions replace the older segment-based heap management. Each region is a small, fixed-size block of memory that the GC can allocate and free independently. Regions are enabled by default in.NET 7+ and improve:

  • Memory return to the OS after usage spikes
  • Heap compaction efficiency
  • Server GC scalability on high-core-count machines

No configuration is needed -- regions are the default. To revert to segments (rarely needed):

{
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Regions": false
    }
  }
}

Generational GC (Gen0/1/2)

How Generations Work

GenerationContainsCollection frequencyCollection cost
Gen0Newly allocated objectsVery frequent (milliseconds)Very cheap (small heap)
Gen1Objects surviving Gen0FrequentCheap
Gen2Long-lived objectsInfrequentExpensive (full heap scan)

Objects promote from Gen0 to Gen1 to Gen2 as they survive collections. The GC budget for Gen0 is tuned dynamically -- when Gen0 fills, a Gen0 collection triggers.

Tuning Principles

  1. Minimize Gen0 allocation rate -- reduce temporary object creation on hot paths. Every allocation contributes to Gen0 pressure.
  2. Avoid mid-life crisis -- objects that live just long enough to promote to Gen1/Gen2 but then become garbage are the most expensive. They survive cheap Gen0 collections and require expensive Gen2 collections to reclaim.
  3. Reduce Gen2 collection frequency -- Gen2 collections cause the longest pauses. Use object pooling, Span, and value types to keep long-lived heap allocations low.

Monitoring Generations

# Real-time GC metrics
dotnet-counters monitor --process-id <PID> \
  --counters System.Runtime[gen-0-gc-count,gen-1-gc-count,gen-2-gc-count,gc-heap-size]
// Programmatic GC observation
var gen0 = GC.CollectionCount(0);
var gen1 = GC.CollectionCount(1);
var gen2 = GC.CollectionCount(2);
var totalMemory = GC.GetTotalMemory(forceFullCollection: false);
var memoryInfo = GC.GetGCMemoryInfo();

logger.LogInformation(
    "GC: Gen0={Gen0} Gen1={Gen1} Gen2={Gen2} Heap={HeapMB:F1}MB",
    gen0, gen1, gen2, totalMemory / (1024.0 * 1024));

Large Object Heap (LOH) and Pinned Object Heap (POH)

LOH

Objects >= 85,000 bytes are allocated on the LOH. LOH collections only happen during Gen2 collections, and by default the LOH is not compacted (causing fragmentation).

// Force LOH compaction (use sparingly -- expensive)
GCSettings.LargeObjectHeapCompactionMode =
    GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();

LOH Fragmentation Prevention

StrategyImplementation
ArrayPool for large arraysArrayPool<byte>.Shared.Rent(100_000)
MemoryPool for IMemoryOwner patternMemoryPool<byte>.Shared.Rent(100_000)
Pre-allocate and reuseCreate large buffers once at startup
Avoid frequent large string concatUse StringBuilder or string.Create

POH (Pinned Object Heap) --.NET 5+

The POH is a dedicated heap for objects that must remain at a fixed memory address (pinned). Before.NET 5, pinning objects on the regular heap prevented compaction. The POH isolates pinned objects so they do not block compaction of Gen0/1/2 heaps.

// Allocate on POH -- useful for I/O buffers passed to native code
byte[] buffer = GC.AllocateArray<byte>(4096, pinned: true);

// The buffer's address will not change, safe for native interop
// and overlapped I/O without explicit GCHandle pinning

Use POH for:

  • I/O buffers passed to native/unmanaged code
  • Memory-mapped file backing arrays
  • Buffers used with Socket.ReceiveAsync (overlapped I/O)

Span/Memory Deep Ownership Patterns

See [skill:dotnet-performance-patterns] for Span/Memory introduction and basic slicing. This section covers ownership semantics and lifetime management for shared buffers.

IMemoryOwner for Pooled Buffers

// Rent from MemoryPool and manage lifetime with IDisposable
using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(4096);
Memory<byte> buffer = owner.Memory[..4096]; // Slice to exact size needed

// Pass the Memory<T> to async I/O
int bytesRead = await stream.ReadAsync(buffer, cancellationToken);
Memory<byte> data = buffer[..bytesRead];

// Process the data
await ProcessDataAsync(data, cancellationToken);
// owner.Dispose() returns the buffer to the pool

Ownership Transfer Pattern

When transferring buffer ownership between components, use IMemoryOwner<T> to make lifetime responsibility explicit:

public sealed class MessageParser
{
    // Caller transfers ownership -- this method is responsible for disposal
    public async Task ProcessAsync(
        IMemoryOwner<byte> messageOwner,
        CancellationToken ct)
    {
        using (messageOwner)
        {
            Memory<byte> data = messageOwner.Memory;
            // Parse and process...
            await HandleMessageAsync(data, ct);
        }
        // Buffer returned to pool on dispose
    }
}

Span Stack Discipline

// Span<T> enforces stack-only usage (ref struct)
// These are compile-time errors:
// Span<byte> field;              // Cannot store in class/struct field
// async Task Foo(Span<byte> s);  // Cannot use in async method
// var list = new List<Span<byte>>(); // Cannot use as generic type argument

// When you need heap storage or async, use Memory<T> instead
public async Task ProcessAsync(Memory<byte> buffer, CancellationToken ct)
{
    // Can use Memory<T> in async methods
    int bytesRead = await stream.ReadAsync(buffer, ct);

    // Convert to Span<T> for synchronous processing within a method
    Span<byte> span = buffer.Span;
    ParseHeader(span[..bytesRead]);
}

ArrayPool and MemoryPool

ArrayPool

ArrayPool<T> reduces GC pressure by reusing array allocations. Always return rented arrays, and never assume the returned array is exactly the requested size.

// Rent and return pattern
byte[] buffer = ArrayPool<byte>.Shared.Rent(minimumLength: 4096);
try
{
    // IMPORTANT: Rented array may be larger than requested
    int bytesRead = await stream.ReadAsync(
        buffer.AsMemory(0, 4096), cancellationToken);
    ProcessData(buffer.AsSpan(0, bytesRead));
}
finally
{
    // clearArray: true when buffer contained sensitive data
    ArrayPool<byte>.Shared.Return(buffer, clearArray: false);
}

Custom Pool Sizing

// Create a custom pool for specific allocation patterns
var pool = ArrayPool<byte>.Create(
    maxArrayLength: 1_048_576,  // 1 MB max array
    maxArraysPerBucket: 50);    // Keep up to 50 arrays per size bucket

// Use for workloads with predictable buffer sizes
byte[] buffer = pool.Rent(65_536);
try
{
    // Process...
}
finally
{
    pool.Return(buffer);
}

MemoryPool

MemoryPool<T> wraps ArrayPool<T> and returns IMemoryOwner<T> for RAII-style lifetime management:

// MemoryPool returns IMemoryOwner<T> -- dispose to return
using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(8192);
Memory<byte> buffer = owner.Memory;

// Slice to exact size (owner.Memory may be larger)
int bytesRead = await stream.ReadAsync(buffer[..8192], ct);
await ProcessAsync(buffer[..bytesRead], ct);
// Dispose returns the underlying array to the pool

Pool Usage Guidelines

GuidelineRationale
Always return rented buffers in finally or usingLeaked buffers defeat the purpose of pooling
Slice to exact size before processingRented arrays may be larger than requested
Use clearArray: true for sensitive dataPool reuse could expose secrets to other consumers
Do not cache rented arrays in long-lived fieldsHolds pool buffers indefinitely, reducing availability
Prefer MemoryPool<T> over raw ArrayPool<T>Disposal-based lifetime is harder to misuse

Weak References and Caching

WeakReference

Weak references allow the GC to collect the target object when no strong references remain. Use for caches where reclamation under memory pressure is acceptable.

public sealed class ImageCache
{
    private readonly ConcurrentDictionary<string, WeakReference<byte[]>> _cache = new();

    public byte[]? TryGet(string key)
    {
        if (_cache.TryGetValue(key, out var weakRef)
            && weakRef.TryGetTarget(out var data))
        {
            return data;
        }
        return null;
    }

    public void Set(string key, byte[] data)
    {
        _cache[key] = new WeakReference<byte[]>(data);
    }

    // Periodically clean up dead references
    public void Purge()
    {
        foreach (var key in _cache.Keys)
        {
            if (_cache.TryGetValue(key, out var weakRef)
                && !weakRef.TryGetTarget(out _))
            {
                _cache.TryRemove(key, out _);
            }
        }
    }
}

When to Use Weak References

  • Large object caches where memory pressure should trigger eviction
  • Caches for expensive-to-compute but recreatable data (image thumbnails, rendered templates)
  • Do NOT use for small objects -- the WeakReference<T> overhead outweighs the benefit

For most caching scenarios, prefer MemoryCache with size limits and expiration policies. Weak references are a last resort when you need GC-driven eviction.


Finalizers vs IDisposable

IDisposable (Preferred)

Implement IDisposable to release unmanaged resources deterministically:

public sealed class NativeBufferWrapper : IDisposable
{
    private IntPtr _handle;
    private bool _disposed;

    public NativeBufferWrapper(int size)
    {
        _handle = Marshal.AllocHGlobal(size);
    }

    public void Dispose()
    {
        if (_disposed) return;
        _disposed = true;

        Marshal.FreeHGlobal(_handle);
        _handle = IntPtr.Zero;
        // No GC.SuppressFinalize needed -- no finalizer
    }
}

Finalizer (Safety Net Only)

Finalizers run on the GC finalizer thread when an object is collected. They are a safety net for unmanaged resources that were not disposed explicitly.

public class UnmanagedResourceHolder : IDisposable
{
    private IntPtr _handle;
    private bool _disposed;

    public UnmanagedResourceHolder(int size)
    {
        _handle = Marshal.AllocHGlobal(size);
    }

    ~UnmanagedResourceHolder()
    {
        Dispose(disposing: false);
    }

    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed) return;
        _disposed = true;

        if (disposing)
        {
            // Free managed resources
        }

        // Free unmanaged resources
        if (_handle != IntPtr.Zero)
        {
            Marshal.FreeHGlobal(_handle);
            _handle = IntPtr.Zero;
        }
    }
}

Finalizer Costs

CostImpact
Objects with finalizers survive at least one extra GCPromotes to Gen1/Gen2, increasing memory pressure
Finalizer thread is single-threadedSlow finalizers block all other finalization
Execution order is non-deterministicCannot depend on other finalizable objects
Not guaranteed to run on process exitCritical cleanup may not execute

Rule: Use sealed classes with IDisposable (no finalizer) unless you own unmanaged handles. Only add a finalizer as a safety net for unmanaged resources.


Memory Pressure Notifications

GC.AddMemoryPressure / RemoveMemoryPressure

Inform the GC about unmanaged memory allocations so it accounts for them in collection decisions:

public sealed class NativeImageBuffer : IDisposable
{
    private readonly IntPtr _buffer;
    private readonly long _size;
    private bool _disposed;

    public NativeImageBuffer(long sizeBytes)
    {
        _size = sizeBytes;
        _buffer = Marshal.AllocHGlobal((IntPtr)sizeBytes);
        GC.AddMemoryPressure(sizeBytes);
    }

    public void Dispose()
    {
        if (_disposed) return;
        _disposed = true;

        Marshal.FreeHGlobal(_buffer);
        GC.RemoveMemoryPressure(_size);
    }
}

GC.GetGCMemoryInfo for Adaptive Behavior

// React to memory pressure in application logic
var memoryInfo = GC.GetGCMemoryInfo();
double loadPercent = (double)memoryInfo.MemoryLoadBytes
    / memoryInfo.TotalAvailableMemoryBytes * 100;

if (loadPercent > 85)
{
    logger.LogWarning("High memory pressure: {Load:F1}%", loadPercent);
    // Shed load: reduce cache sizes, reject non-critical requests
}

Memory Profiling

dotMemory (JetBrains)

dotMemory provides heap snapshots and allocation tracking with a visual UI. Use it for investigating memory leaks and high-allocation hot paths.

Workflow:

  1. Attach dotMemory to the running process (or launch with profiling enabled)
  2. Capture a baseline snapshot after application warm-up
  3. Execute the scenario under investigation
  4. Capture a second snapshot
  5. Compare snapshots to identify retained objects and growth

Key views:

  • Sunburst -- shows allocation tree by type hierarchy
  • Dominator tree -- shows which objects prevent GC of retained memory
  • Survived objects -- objects allocated between snapshots that survived GC

PerfView

PerfView is a free Microsoft tool for detailed GC and allocation analysis. It uses ETW (Event Tracing for Windows) events for low-overhead profiling.

# Collect GC and allocation events for 30 seconds
PerfView.exe /GCCollectOnly /MaxCollectSec:30 collect

# Collect allocation stacks (higher overhead)
PerfView.exe /ClrEvents:GC+Stack /MaxCollectSec:30 collect

Key PerfView views:

  • GCStats -- GC pause times, generation counts, promotion rates, fragmentation
  • GC Heap Alloc Stacks -- call stacks responsible for allocations
  • Any Stacks -- CPU sampling for identifying hot methods

Profiling Workflow

  1. Identify the symptom -- high memory usage, growing Gen2, frequent Gen2 collections, LOH fragmentation
  2. Monitor with dotnet-counters (see [skill:dotnet-profiling]) to confirm GC metrics match the symptom
  3. Profile with dotMemory or PerfView to identify the objects and allocation sites
  4. Apply fixes -- pool buffers, use Span, reduce allocations, fix leaks
  5. Validate with BenchmarkDotNet (see [skill:dotnet-benchmarkdotnet]) [MemoryDiagnoser] to confirm improvement
  6. Monitor in production via OpenTelemetry runtime metrics (see [skill:dotnet-observability])

Agent Gotchas

  1. Do not default to workstation GC for ASP.NET Core applications -- server GC is the default and correct choice for web workloads. Workstation GC has lower throughput on multi-core servers. Only override for specific latency-sensitive scenarios.
  2. Do not forget to return ArrayPool buffers -- leaked pool buffers are worse than regular allocations because they hold pool capacity indefinitely. Always use try/finally or IMemoryOwner<T> with using.
  3. Do not assume rented arrays are the requested size -- ArrayPool<T>.Rent() may return an array larger than requested. Always slice to the exact size needed before processing.
  4. Do not add finalizers to classes that only use managed resources -- finalizers promote objects to Gen1/Gen2 and add overhead to GC. Use sealed class with IDisposable (no finalizer) for managed-only cleanup.
  5. Do not call GC.Collect() in production code -- forcing full collections causes long pauses and disrupts the GC's dynamic tuning. Use GC.AddMemoryPressure() to hint at unmanaged memory instead.
  6. Do not ignore LOH fragmentation -- large arrays (>= 85,000 bytes) allocated and freed repeatedly fragment the LOH. Use ArrayPool<T> to rent and return large buffers instead of allocating new arrays.
  7. Do not cache IMemoryOwner in long-lived fields without disposal tracking -- the underlying pooled buffer is held indefinitely, preventing pool reuse. Transfer ownership explicitly or limit cache lifetimes.

References

平台分布

Codex

35.14%
按下载量换算50

Claude

31.18%
按下载量换算44

Cursor

20.95%
按下载量换算30

Gemini CLI

10.04%
按下载量换算14

安全审计

Gen Agent Trust Hub

通过

Socket

未通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。来源安全扫描存在提醒或未通过项,安装前请自行复核。目前只有一个公开来源,建议同时核对源仓库。

继续浏览同类 Skills