Skip to main content
When initializing accounts, you must specify the space (size in bytes) required.

Basic formula

space = 8 (discriminator) + account_data_size

Type sizes

TypeSize (bytes)
bool1
u8/i81
u16/i162
u32/i324
u64/i648
u128/i12816
Pubkey32
String4 + length
Vec<T>4 + (size_of(T) * length)
Option<T>1 + size_of(T)

InitSpace derive

Use InitSpace for automatic calculation:
#[account]
#[derive(InitSpace)]
pub struct Data {
    pub value: u64,        // 8
    pub owner: Pubkey,     // 32
    pub name: String,      // 4 + len
}

// Usage
#[account(init, payer = user, space = 8 + Data::INIT_SPACE)]
pub data: Account<'info, Data>

String and Vec lengths

Specify max lengths with attributes:
#[account]
#[derive(InitSpace)]
pub struct Data {
    #[max_len(50)]
    pub name: String,
    #[max_len(10)]
    pub items: Vec<u64>,
}

Manual calculation

#[account(init, payer = user, space = 8 + 32 + 8)]
pub data: Account<'info, Data>

#[account]
pub struct Data {
    pub owner: Pubkey,  // 32
    pub count: u64,     // 8
}
// Total: 8 (discriminator) + 32 + 8 = 48 bytes

Best practices

Always use InitSpace derive to avoid calculation errors.
Allocate enough space for dynamic types (String, Vec) based on expected max size.

Build docs developers (and LLMs) love