Space Reference

This reference tells you how much space you should allocate for an account.

This only applies to accounts that don't use zero-copy. zero-copy uses repr(C) with a pointer cast, so there the C layout applies.

In addition to the space for the account data, you have to add 8 to the space constraint for Anchor's internal discriminator (see the example).

Type chart

TypesSpace in bytesDetails/Example
bool1would only require 1 bit but still uses 1 byte
[T;amount]space(T) * amounte.g. space([u16;32]) = 2 * 32 = 64
Vec<T>4 + (space(T) * amount)Account size is fixed so account should be initialized with sufficient space from the beginning
String4 + length of string in bytesAccount size is fixed so account should be initialized with sufficient space from the beginning
Option<T>1 + (space(T))
Enum1 + Largest Variant Sizee.g. Enum { A, B { val: u8 }, C { val: u16 } } -> 1 + space(u16) = 3
f324serialization will fail for NaN
f648serialization will fail for NaN


pub struct MyData {
    pub val: u16,
    pub state: GameState,
    pub players: Vec<Pubkey> // we want to support up to 10 players

impl MyData {
    pub const MAX_SIZE: usize = 2 + (1 + 32) + (4 + 10 * 32);

#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq, Eq)]
pub enum GameState {
    Won { winner: Pubkey },

pub struct InitializeMyData<'info> {
    // Note that we have to add 8 to the space for the internal anchor
    #[account(init, payer = signer, space = 8 + MyData::MAX_SIZE)]
    pub acc: Account<'info, MyData>,
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>

The InitSpace macro

Sometimes it can be difficult to calculate the initial space of an account. This macro will add an INIT_SPACE constant to the structure. It is not necessary for the structure to contain the #[account] macro to generate the constant. Here's an example:

pub struct ExampleAccount {
    pub data: u64,
    pub string_one: String,
    #[max_len(10, 5)]
    pub nested: Vec<Vec<u8>>,

pub struct Initialize<'info> {
    pub payer: Signer<'info>,
    pub system_program: Program<'info, System>,
    #[account(init, payer = payer, space = 8 + ExampleAccount::INIT_SPACE)]
    pub data: Account<'info, ExampleAccount>,

A few important things to know:

  • Don't forget the discriminator when defining space
  • The max_len length represents the length of the structure, not the total length. (ie: the max_len of a Vec<u32> will be max_len * 4)
JavaScript Anchor Types