AgentSkillsCN

profiling-memory-allocations

实验性分配剖析器可在客语言层面追踪内存分配情况,清晰展示各分配站点、对象类型以及内存压力分布模式。借助该工具,您可以快速定位高内存占用的热点区域,发现不必要的对象创建,并深入理解程序的内存行为。若热点循环中存在大量内存分配,而逃逸分析未能有效消除此类开销,则意味着这正是优化的绝佳契机。

SKILL.md
--- frontmatter
name: profiling-memory-allocations
description: Experimental allocation profiler tracking memory allocations at guest-language level. Shows allocation sites, object types, and memory pressure patterns. Use to identify allocation-heavy locations, find unnecessary object creation, and understand memory behavior. High allocations in hot loops = optimization opportunity if escape analysis isn't eliminating them.

Profiling Memory Allocations

Experimental allocation profiler tracking memory allocations at the guest-language level. Identifies allocation-heavy locations and unnecessary object creation.

When to Use This Skill

  • Identify allocation-heavy locations in hot loops
  • Find unnecessary object creation
  • Understand memory behavior patterns
  • Detect escape analysis opportunities

Note: High allocations in hot loops = optimization opportunity if escape analysis isn't eliminating them.

Quick Start

bash
# Basic memory tracing
<launcher> --memtracer <program>

# With stack traces
<launcher> --memtracer --memtracer.TraceStackTraces=true <program>

# Filter by allocation count
<launcher> --memtracer --memtracer.TraceMem=1000 <program>

⚠️ REQUIRED: Fermi Verification (Every Tool Invocation)

Before running:

  • Pre-calculate: Expected allocation count (estimate based on loops/iterations)
  • Smoke test: <launcher> --memtracer -c 'var a = 👉1, 2, 3👈;' → Verify shows allocations

After running:

  • Validate: Actual vs estimate within 1 order of magnitude? YES / NO
  • If NO: STOP - Debug tool before proceeding (test with known allocating code)
  • Save output: tool-outputs/memtracer-[benchmark].txt

Gate: All boxes checked? → Proceed to analysis

Key Options

OptionDescription
--memtracerEnable memory tracer
--memtracer.TraceStackTraces=trueInclude allocation stack traces
--memtracer.TraceMem=<bytes>Filter by minimum allocation
--memtracer.Output=jsonJSON output format
--memtracer.OutputFile=<file>Save to file

Understanding Output

Sample Output

code
Memory Tracer. Recorded 45678 allocations.

Source Location           || Count   || Size      || Type
queens.lox:42            || 50000   || 400KB     || LoxArray
queens.lox:55            || 25000   || 200KB     || LoxObject

Interpretation

Allocation Patterns

PatternStatusAction
Few allocations in hot path✅ GoodEscape analysis working
Many allocations in hot loop⚠️ ProblemObjects escaping
Allocations only in cold path✅ OKExpected behavior

High Allocation Sites

If hot loop shows many allocations:

  1. Check if objects escape the loop
  2. Verify escape analysis is running
  3. Consider restructuring to avoid allocation

Common Issues

Issue 1: Array Allocations in Loop

code
queens.lox:42  || 50000 allocations

Cause: Creating arrays inside hot loop Fix: Reuse arrays, or ensure they're eliminated by EA

Issue 2: Object Allocations for Temporaries

code
Point creation || 100000 allocations

Cause: Temporary objects not eliminated Fix: Keep objects local, avoid escaping references

Integration with Other Skills

FindingNext Skill
High allocationsanalyzing-compiler-graphs (check EA)
Unclear sourceAdd stack traces
Performance issueprofiling-with-cpu-sampler

Related Skills

  • profiling-with-cpu-sampler - Time-based profiling
  • analyzing-compiler-graphs - Check escape analysis
  • detecting-performance-warnings - Find barriers
  • establishing-benchmark-baseline - Set expectations

Reference

bash
<launcher> --help:memtracer