Space Calculation
Correct space calculation prevents failed inits and rent surprises. Sum discriminators, Borsh fields, or size_of::<Pod>(), then query Rent for lamports.
Search across all documentation pages
Correct space calculation prevents failed inits and rent surprises. Sum discriminators, Borsh fields, or size_of::<Pod>(), then query Rent for lamports.
pub const SPACE: usize = 8 + User::LEN;
let lamports = Rent::get()?.minimum_balance(SPACE);When to reach for this:
space = attributes.#[derive(BorshSerialize, BorshDeserialize)]
pub struct User { pub authority: Pubkey, pub points: u64 }
impl User { pub const LEN: usize = 32 + 8; }
pub const USER_SPACE: usize = 8 + User::LEN; // disc + bodyWhat this demonstrates:
SPACE = disc + sum(field sizes) + variable length caps.
4 + max_entries * entry_size.
// Host test: assert_eq!(serialized.len(), USER_SPACE - 8);
| Alternative | Use When | Don't Use When |
|---|---|---|
| Anchor InitSpace | Auto | Manual native |
| Host serialize measure | Ground truth | Runtime only |
| Excel rent calculator | Planning | Not code substitute |
Rent::minimum_balance.
realloc pays delta.
165 bytes classic SPL.
On-chain only data len.
10 MB.
Only lamports holder.
Macro expands at compile time.
32 * n.
Avoid - use fixed bytes.
Unit test host.
solana rent.
Verify create with SPACE.
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.
Reviewed by Chris St. John·Last updated Jul 19, 2026