Space & Rent
Solana accounts must be rent-exempt. Anchor init uses your space attribute to allocate bytes including the 8-byte discriminator.
Recipe
#[account]
pub struct User {
pub authority: Pubkey,
pub points: u64,
}
// space = 8 + User::INIT_SPACE
#[account(init, payer = payer, space = 8 + User::INIT_SPACE)]
pub user: Account<'info, User>,When to reach for this: You add fields to on-chain state or create new account types.
Working Example
#[account]
#[derive(InitSpace)]
pub struct Config {
pub admin: Pubkey,
#[max_len(64)]
pub name: String,
pub fee_bps: u16,
}
#[account(init, payer = payer, space = 8 + Config::INIT_SPACE)]
pub config: Account<'info, Config>,What this demonstrates:
InitSpacederive calculates size at compile time- 8-byte discriminator is mandatory for Anchor accounts
- Payer supplies lamports for rent exemption
max_lenon strings/vecs bounds allocation
Deep Dive
Sizing Rules
- Fixed fields: sum of Borsh sizes.
String/Vec: needmax_lenwithInitSpace.- Zero-copy accounts use different sizing (no Borsh on chain).
Rent
Rent-exempt minimum depends on allocated size. Use Rent::get()?.minimum_balance(space) in tests to assert funding.
Gotchas
- Forgetting discriminator in space - Account too small; serialize panics.. Fix: Always add 8 to
INIT_SPACE. - Unbounded String - No
max_len; size unknown.. Fix: Use#[max_len(n)]with InitSpace. - Undersized Vec - Grows beyond allocation.. Fix: Preallocate max length or use
realloc. - Wrong INIT_SPACE after field add - Upgrade breaks new inits.. Fix: Bump version and migrate.
- Closing without reclaiming rent - Users lose SOL.. Fix: Use
close = recipient.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| realloc constraint | Growing accounts | Fixed-size known upfront |
| zero_copy | Large fixed structs | Small config accounts |
| Manual space calc | Non-Anchor | InitSpace derive |
FAQs
What Anchor version does this site use?
0.32.1 for anchor-lang, Anchor CLI, and examples in this section.
Do I need Solana CLI alongside Anchor?
Yes. Solana CLI 3.0.10 handles keypairs, airdrops, and solana program inspection.
Where does the IDL live after build?
target/idl/<program>.json in your workspace.
Can I mix UncheckedAccount with Signer?
Yes, but every unchecked field needs explicit constraints or handler checks.
How do I test without devnet?
Use anchor test with Surfpool 0.12.0 or LiteSVM 0.6.x in CI.
What is the 8-byte prefix on account data?
Anchor account discriminator; do not strip it when sizing space.
Should I commit generated IDL?
Yes, or publish on-chain IDL so clients have a canonical source.
How do I debug constraint failures?
Run with logs; Anchor prints constraint name and account index.
Does Anchor work on Agave 4.1.1?
Yes. This stack targets Agave validators with Solana CLI 3.0.10.
What should I read next in this section?
See sibling articles linked in Related for deeper space rent topics.
Related
- realloc in Anchor - grow accounts
- Space Calculation - serialization sizing
- init & init_if_needed - allocation
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.