Marketplace
memory-management
Game memory optimization, object pooling, garbage collection tuning, and efficient resource management for target platforms.
$ Installieren
git clone https://github.com/pluginagentmarketplace/custom-plugin-game-developer /tmp/custom-plugin-game-developer && cp -r /tmp/custom-plugin-game-developer/skills/memory-management ~/.claude/skills/custom-plugin-game-developer// tip: Run this command in your terminal to install the skill
SKILL.md
name: memory-management version: "2.0.0" description: | Game memory optimization, object pooling, garbage collection tuning, and efficient resource management for target platforms. sasmp_version: "1.3.0" bonded_agent: 02-game-programmer bond_type: PRIMARY_BOND
parameters:
- name: platform type: string required: false validation: enum: [pc, console, mobile, vr, web]
- name: issue_type type: string required: false validation: enum: [leak, fragmentation, gc_spikes, budget_exceeded]
retry_policy: enabled: true max_attempts: 3 backoff: exponential
observability: log_events: [start, complete, error, gc_collect] metrics: [heap_size_mb, gc_time_ms, allocation_rate, pool_usage]
Memory Management
Memory Architecture
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â GAME MEMORY LAYOUT â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â STACK (Fast, Auto-managed): â
â ââ Local variables â
â ââ Function parameters â
â ââ Return addresses â
â â
â HEAP (Slower, Manual/GC-managed): â
â ââ Dynamic allocations (new/malloc) â
â ââ Game objects â
â ââ Asset data â
â â
â STATIC (Fixed at compile time): â
â ââ Global variables â
â ââ Static class members â
â ââ Constant data â
â â
â VRAM (GPU Memory): â
â ââ Textures â
â ââ Meshes â
â ââ Render targets â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Platform Memory Budgets
MEMORY BUDGET GUIDELINES:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PLATFORM â TOTAL â GAME LOGIC â ASSETS â BUFFER â
ââââââââââââââââââŒâââââââââââŒâââââââââââââŒâââââââââââŒâââââââââ€
â Mobile Low â 512 MB â 50 MB â 350 MB â 112 MB â
â Mobile High â 2 GB â 200 MB â 1.5 GB â 300 MB â
â Console â 8 GB â 500 MB â 6 GB â 1.5 GB â
â PC Min â 4 GB â 300 MB â 3 GB â 700 MB â
â PC High â 16 GB â 1 GB â 12 GB â 3 GB â
â VR â 8 GB â 400 MB â 6 GB â 1.6 GB â
ââââââââââââââââââŽâââââââââââŽâââââââââââââŽâââââââââââŽâââââââââ
VRAM BUDGETS:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Mobile: 512 MB - 1 GB â
â Console: 8-12 GB (shared with RAM) â
â PC Low: 2-4 GB â
â PC High: 8-16 GB â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Object Pooling
// â
Production-Ready: Generic Object Pool
public class ObjectPool<T> where T : class
{
private readonly Stack<T> _pool;
private readonly Func<T> _createFunc;
private readonly Action<T> _onGet;
private readonly Action<T> _onReturn;
private readonly int _maxSize;
public int CountActive { get; private set; }
public int CountInPool => _pool.Count;
public ObjectPool(
Func<T> createFunc,
Action<T> onGet = null,
Action<T> onReturn = null,
int initialSize = 10,
int maxSize = 100)
{
_createFunc = createFunc;
_onGet = onGet;
_onReturn = onReturn;
_maxSize = maxSize;
_pool = new Stack<T>(initialSize);
// Pre-warm pool
for (int i = 0; i < initialSize; i++)
{
_pool.Push(_createFunc());
}
}
public T Get()
{
T item = _pool.Count > 0 ? _pool.Pop() : _createFunc();
_onGet?.Invoke(item);
CountActive++;
return item;
}
public void Return(T item)
{
if (item == null) return;
_onReturn?.Invoke(item);
CountActive--;
if (_pool.Count < _maxSize)
{
_pool.Push(item);
}
// If pool is full, let GC collect the item
}
public void Clear()
{
_pool.Clear();
CountActive = 0;
}
}
// Usage Example: Bullet Pool
public class BulletManager : MonoBehaviour
{
private ObjectPool<Bullet> _bulletPool;
void Awake()
{
_bulletPool = new ObjectPool<Bullet>(
createFunc: () => Instantiate(bulletPrefab).GetComponent<Bullet>(),
onGet: bullet => bullet.gameObject.SetActive(true),
onReturn: bullet => bullet.gameObject.SetActive(false),
initialSize: 50,
maxSize: 200
);
}
public Bullet SpawnBullet(Vector3 position, Vector3 direction)
{
var bullet = _bulletPool.Get();
bullet.Initialize(position, direction);
bullet.OnDestroyed += () => _bulletPool.Return(bullet);
return bullet;
}
}
Garbage Collection Optimization
GC SPIKE PREVENTION:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â AVOID IN UPDATE/HOT PATHS: â
â â new object() â
â â string concatenation ("a" + "b") â
â â LINQ queries (ToList(), Where(), etc.) â
â â Boxing value types â
â â Closures/lambdas capturing variables â
â â foreach on non-struct enumerators â
â â
â DO INSTEAD: â
â â Object pooling â
â â StringBuilder for strings â
â â Pre-allocated collections â
â â Struct-based data â
â â Cache delegates â
â â for loops with index â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
// â
Production-Ready: Allocation-Free Patterns
public class AllocationFreePatterns
{
// â BAD: Allocates every frame
void BadUpdate()
{
string status = "Health: " + health + "/" + maxHealth; // Allocates
var enemies = allEntities.Where(e => e.IsEnemy).ToList(); // Allocates
foreach (var enemy in enemies) { } // May allocate enumerator
}
// â GOOD: Zero allocations
private StringBuilder _sb = new StringBuilder(64);
private List<Entity> _enemyCache = new List<Entity>(100);
void GoodUpdate()
{
// Reuse StringBuilder
_sb.Clear();
_sb.Append("Health: ").Append(health).Append("/").Append(maxHealth);
// Reuse list, avoid LINQ
_enemyCache.Clear();
for (int i = 0; i < allEntities.Count; i++)
{
if (allEntities[i].IsEnemy)
_enemyCache.Add(allEntities[i]);
}
// Index-based iteration
for (int i = 0; i < _enemyCache.Count; i++)
{
ProcessEnemy(_enemyCache[i]);
}
}
}
Memory Profiling
PROFILING WORKFLOW:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â 1. BASELINE: Measure memory at known state â
â â Startup, menu, gameplay, level transition â
â â
â 2. MONITOR: Track over time â
â â Memory growth indicates leaks â
â â Spikes indicate heavy allocations â
â â
â 3. SNAPSHOT: Capture heap at suspicious moments â
â â Compare snapshots to find what's growing â
â â
â 4. TRACE: Find allocation source â
â â Stack trace shows where allocation happened â
â â Identify hot paths â
â â
â 5. FIX: Apply optimization â
â â Pool, cache, or eliminate allocation â
â â
â 6. VERIFY: Confirm fix worked â
â â Re-profile same scenario â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
PROFILING TOOLS:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Unity: â
â ⢠Memory Profiler (Package) â
â ⢠Profiler window (Memory section) â
â ⢠Deep Profile mode â
â â
â Unreal: â
â ⢠Unreal Insights â
â ⢠memreport command â
â ⢠stat memory â
â â
â Native: â
â ⢠Valgrind (Linux) â
â ⢠Instruments (macOS) â
â ⢠Visual Studio Diagnostic Tools â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Asset Streaming
STREAMING STRATEGY:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â STREAMING ZONES â
â â
â [Loaded] [Loading...] [Unloaded] [Unloaded] â
â â² â² â â â
â â â â â â
â âââââââââââââââââââââââââââââââââââââââââââââââââââ â
â Player Trigger Far Zone Very Far â
â Position â
â â
â STREAMING RULES: â
â ⢠Load when player approaches (predictive) â
â ⢠Unload when player far away + timeout â
â ⢠Priority: Visible > Nearby > Background â
â ⢠Async loading to avoid hitches â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
// â
Production-Ready: Asset Streaming Manager
public class StreamingManager : MonoBehaviour
{
[SerializeField] private float loadDistance = 50f;
[SerializeField] private float unloadDistance = 100f;
[SerializeField] private float unloadDelay = 5f;
private Dictionary<string, StreamingZone> _zones = new();
private Queue<AsyncOperation> _loadQueue = new();
void Update()
{
Vector3 playerPos = Player.Position;
foreach (var zone in _zones.Values)
{
float distance = Vector3.Distance(playerPos, zone.Center);
if (distance < loadDistance && !zone.IsLoaded)
{
StartCoroutine(LoadZoneAsync(zone));
}
else if (distance > unloadDistance && zone.IsLoaded)
{
StartCoroutine(UnloadZoneDelayed(zone, unloadDelay));
}
}
}
private IEnumerator LoadZoneAsync(StreamingZone zone)
{
zone.State = ZoneState.Loading;
var operation = SceneManager.LoadSceneAsync(zone.SceneName, LoadSceneMode.Additive);
operation.allowSceneActivation = false;
while (operation.progress < 0.9f)
{
yield return null;
}
operation.allowSceneActivation = true;
zone.State = ZoneState.Loaded;
}
}
ð§ Troubleshooting
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Memory keeps growing over time (leak) â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â DEBUG: â
â â Take memory snapshots at intervals â
â â Compare snapshots to find growing objects â
â â Check for missing unsubscribes (events) â
â â Look for collections that never clear â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â SOLUTIONS: â
â â Implement IDisposable pattern â
â â Use weak references for caches â
â â Clear collections when changing scenes â
â â Unsubscribe from events in OnDestroy â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: GC causing frame spikes â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â DEBUG: â
â â Profile with GC.Alloc markers â
â â Look for allocations in Update/FixedUpdate â
â â Check for string operations in hot paths â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â SOLUTIONS: â
â â Implement object pooling â
â â Use structs instead of classes where possible â
â â Pre-allocate collections with known capacity â
â â Spread GC across frames (incremental GC) â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Out of memory on mobile â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â DEBUG: â
â â Check texture memory usage â
â â Look for duplicate assets â
â â Monitor during level transitions â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â SOLUTIONS: â
â â Reduce texture resolution â
â â Implement aggressive streaming â
â â Unload unused assets (Resources.UnloadUnusedAssets) â
â â Use compressed texture formats â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Hitches during level loading â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ€
â SOLUTIONS: â
â â Use async loading (LoadSceneAsync) â
â â Spread instantiation across frames â
â â Pre-warm object pools during loading screen â
â â Stream assets instead of loading all at once â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Memory Optimization Checklist
| Area | Technique | Impact | Effort |
|---|---|---|---|
| Objects | Object Pooling | High | Medium |
| Strings | StringBuilder | Medium | Low |
| Collections | Pre-allocation | Medium | Low |
| Assets | Streaming | High | High |
| Textures | Compression | High | Low |
| GC | Incremental GC | Medium | Low |
| Events | Weak References | Low | Medium |
Use this skill: When optimizing memory usage, reducing frame stutters, or supporting mobile platforms.
Repository

pluginagentmarketplace
Author
pluginagentmarketplace/custom-plugin-game-developer/skills/memory-management
1
Stars
0
Forks
Updated1d ago
Added1w ago