Coding
AI Prompt for Memory Optimization
Cut memory usage with this AI prompt: allocation hotspot analysis, retention fixes, right-sized structures, GC tuning, budgets, and load tests.
The prompt
Role: You are a memory optimization expert for [LANGUAGE] on [RUNTIME]. Context: Symptom: [SYMPTOM, e.g. 2s GC pauses, OOM at 10k users]. Heap/config: [MEMORY_CONFIG]. Suspect code: [CODE]. Task: 1) Analyze memory behavior: allocation hotspots, object retention (what keeps memory alive), duplicated data, oversized structures. 2) Fix the code: right-size collections, eliminate retention leaks, reduce allocation in hot paths, use primitive/specialized types where they matter. 3) Tune the runtime: heap sizing, GC algorithm choice for [RUNTIME] with reasoning. 4) Define memory BUDGETS per component and the alerting to enforce them. 5) Provide a load test that reproduces the pressure. Format: Analysis first, then code fixes, then runtime tuning, then budgets + alerting, then the load test. Ask the AI to define the steady-state memory signature: what 'healthy' looks like on your dashboard after the fix. Without a baseline, every future fluctuation becomes a false alarm. Constraints: Measure with a profiler before and after — no tuning by vibes. Prefer code fixes over heap increases; bigger heaps delay GC but lengthen pauses. Every tuning change needs its rollback noted.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [RUNTIME] [SYMPTOM] [MEMORY_CONFIG] [CODE]
Example input
LANGUAGE=Java, RUNTIME=JVM heap 4GB, SYMPTOM=GC pauses 2s, frequent full GCs, SUSPECT=large in-memory caches + string duplication
How to use this prompt
Describe the GC symptoms (pause times, frequency) — they diagnose the problem class. Name the heap size; tuning is relative to it.
Tips for the best results
- Profile allocations, not just heap size — the allocation rate drives GC pressure.
- Ask for the retention analysis; leaks are about references, not just volume.
- Set the memory budget alerts before you need them.
Frequently asked questions
Code or GC tuning?
It analyzes allocation patterns and retention, then fixes the code AND the GC/heap config — both levers matter.
Is adding more heap ever the right fix?
As a stopgap, rarely as a solution — bigger heaps mean longer GC pauses. The prompt prefers code fixes and treats heap increases as temporary.