realloc & Growing Accounts
Programs can grow (or shrink) account data via the realloc syscall/CPI, paying additional rent from a payer. Required when schemas grow or dynamic collections expand.
Recipe
account.realloc(new_len, false)?;
account.transfer_required_rent(payer)?; // conceptually - use realloc CPI patternWhen to reach for this:
- Adding fields in program upgrade.
- Growing dynamic vector storage.
- Shrinking on data prune.
Working Example
// Native: use solana_program::program::realloc or system CPI patterns per SDK
// After realloc, zero new bytes and initialize new fields
let new_len = old_len + 8;
// realloc account to new_len with payer funding rent deltaWhat this demonstrates:
- Rent delta must be funded.
- New bytes must be initialized.
- Shrink returns rent to destination.
Deep Dive
Safety
- Reject shrink if would truncate live fields.
- Zero new region to avoid leak of stale bytes.
Anchor
realloc constraint in 0.32.1.
Rust Notes
// Document max_len to cap griefing.Gotchas
- Realloc without payer funds - Fails mid-upgrade.. Fix: Ensure payer signer with lamports.
- Uninitialized new bytes - Deserialize garbage.. Fix: Zero init.
- Shrink active data - Corruption.. Fix: Forbid or migrate first.
- Unbounded growth - Rent griefing.. Fix: Cap max_len.
- Realloc in loop - CU expensive.. Fix: Batch growth.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| New account migration | Safer | User friction |
| Fixed max size upfront | Wastes rent early | Simple |
| Off-chain storage | Cheap | Trust model |
FAQs
Who pays rent increase?
Payer signer you specify.
Max growth?
10 MB account cap.
Shrink returns rent?
To chosen recipient.
Anchor realloc?
Attribute on account.
Concurrent realloc?
Tx serialization per account.
Zero new space?
Required security practice.
Migration ix?
Common upgrade path.
LiteSVM realloc?
Supported.
CU cost?
Profile realloc ix.
Client must sign?
Payer yes.
Partial realloc?
Set absolute new length.
Immutable program?
Plan size headroom early.
Related
- Data Versioning & Migration - Schema
- Space Calculation - Planning
- Upgrade-Safe Data Design - Forward compat
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.