Sign in

Account space and layout

Suggest an edit

An account's data field is a fixed slab of bytes. When you initialize an account, you tell the runtime how many bytes you want and you pay rent for that many. Every field on your Rust struct has to fit into that slab in a known, fixed amount of room: 8 bytes for a discriminator that says what type the account is, plus the size of each field.

A Solana account is a fixed-size struct

The data field of an account is allocated to a specific byte count when the account is created. Rent was paid for that many bytes and that's how many the chain reserves for the account. You can write different values into those bytes, but there is no implicit growth.

So you decide the size of an account at init. Changing it later takes an explicit realloc in a separate instruction, which costs additional rent when the account grows. The account closure and realloc lesson covers it. Programs that need to store more data than fits in one account use multiple accounts, each at its own PDA, each sized at creation.

Byte layout of a Vault account: discriminator, authority, total, bumpThe diagram maps the Rust Vault struct to bytes in the account's data field: an 8-byte discriminator, a 32-byte authority (Pubkey), an 8-byte total (u64), and a 1-byte bump, adding up to 49 bytes. It shows that this size is fixed once the account is created and cannot grow later. The byte layout of a Vault account your Rust struct #[account] pub struct Vault { pub authority: Pubkey, pub total: u64, pub bump: u8, } what lives in the account's data field byte offset: 0 8 40 48 discriminator 8 bytes authority (Pubkey) 32 bytes total (u64) 8 bytes bump 1 byte total size: 8 + 32 + 8 + 1 = 49 bytes that's the value you put in: space = 8 + Vault::INIT_SPACE // 8 + 41 = 49 Once created, this size is fixed The account's data field is exactly 49 bytes from the moment it's initialized. You cannot add a new field later. You cannot grow the struct implicitly. A Solana account is a fixed-size struct. Size is decided at init and lives with the account.

For this Vault the sum is 8 + 32 + 8 + 1 = 49 bytes, and that's the value you pass to space when you initialize the account.

The first 8 bytes say which type the account is

Every Anchor account starts with 8 bytes that aren't part of your struct: a discriminator. The discriminator is a hash derived from the account type's name, stored at the beginning of the data field, and checked by Anchor before deserializing the rest. Its job is to make sure the bytes you're about to interpret as a Vault were written by a Vault-shaped instruction, and not by some other instruction that happens to produce 49 bytes.

Without the discriminator, a class of bugs called type confusion becomes possible. If you have two account types Vault and Pool that happen to be the same size, an attacker could initialize a Pool account, pass it to your Vault instruction, and your deserialization would succeed because the bytes parse cleanly as either struct, but with different field meanings. The attacker has effectively turned a Pool into a Vault for the purposes of one call, and any check you wrote that depends on the type of the account silently fails.

The first 8 bytes of a Vault account are the hash of "account:Vault". The first 8 bytes of a Pool account are the hash of "account:Pool". When Anchor sees Account<'info, Vault> in your struct, it reads those 8 bytes, compares them to the Vault discriminator, and rejects the account on a mismatch. No matter what's in the rest of the data, you can be sure the account is the type you asked for.

The discriminator is stored in the account's data, so space always starts with 8. Leave it out and the account is 8 bytes short, and the instruction that creates it fails. Right after allocating the account, Anchor reads it back as a Vault, and the bytes after the discriminator are too few to hold one, so the read stops with AccountDidNotDeserialize.

Field sizes are fixed, and Anchor adds them up

Computing the size of an account by hand means knowing the size of each Rust type that goes into it.

Byte sizes of Rust types for account spaceA table lists Rust types such as bool, integers, Pubkey, arrays, Option<T>, String, and Vec<T>, showing the byte size and notes for each. It ends with the rule: total space equals an 8-byte discriminator plus the sum of all field sizes, which #[derive(InitSpace)] can compute automatically. How many bytes does each Rust type take? type size notes bool 1 byte stored as 0 or 1 u8, i8 1 byte u16, i16 2 bytes u32, i32, f32 4 bytes u64, i64, f64 8 bytes u128, i128 16 bytes Pubkey 32 bytes an address [T; N] N × size_of(T) fixed-size array, length in the type Option<T> 1 + size_of(T) 1-byte tag (Some/None) + the inner variable-length: need explicit bounds String 4 + max_len bytes 4-byte length prefix + the UTF-8 bytes requires #[max_len(N)] Vec<T> 4 + N × size_of(T) 4-byte length prefix + N elements requires #[max_len(N)] without max_len, Anchor doesn't know how much to allocate. you must tell it. total space = 8 (discriminator) + sum of all fields Or let #[derive(InitSpace)] compute it for you. The macro reads your fields and adds them up.

You almost never compute this by hand. Anchor provides a #[derive(InitSpace)] macro that adds an INIT_SPACE constant to your struct, equal to the sum of the field sizes. You then write space = 8 + Vault::INIT_SPACE, and the compiler computes the right number. When you change the struct's fields, the constant updates automatically.

A Vec or String needs a maximum length

String and Vec<T> are variable-length: in normal Rust, they grow as you push to them. The account's data field does not grow when you push, so Anchor needs to know, at init time, how many bytes to reserve for each variable-length field.

You tell it with #[max_len(N)], an attribute on the field that gives the maximum number of elements the field will ever hold. The space allocated for the field is 4 + max_len × size_of_element, where the 4 bytes hold the current length and the rest hold the elements. #[derive(InitSpace)] uses that number when it computes INIT_SPACE:

rust
#[account]
#[derive(InitSpace)]
pub struct Proposal {
    #[max_len(100)]
    pub votes: Vec<Pubkey>,
}

#[derive(Accounts)]
pub struct CreateProposal<'info> {
    #[account(init, payer = creator, space = 8 + Proposal::INIT_SPACE)]
    pub proposal: Account<'info, Proposal>,
    #[account(mut)]
    pub creator: Signer<'info>,
    pub system_program: Program<'info, System>,
}

Here Proposal::INIT_SPACE is 4 + 100 × 32 = 3,204, and the account gets 8 + 3,204 = 3,212 bytes.

#[max_len] only sizes the account and does not stop a push. Inside the handler votes is an ordinary Vec in memory, and a 101st push succeeds there. The failure comes when the handler returns and Anchor writes the account back into its 3,212 bytes: the vector no longer fits, and the transaction fails with AccountDidNotSerialize. To fail with your own error instead, check votes.len() before you push.

Unbounded Vec vs #[max_len(100)] bounded Vec account spaceThe diagram compares two versions of a Proposal account with a votes: Vec<Pubkey> field. On the left, no bounds are set, so at init time Anchor cannot compute the space and a guess is either too low (push overflows, tx reverts) or too high (wasted rent). On the right, #[max_len(100)] fixes space at 8 + 4 + 10032 = 3,212 bytes, so pushes succeed up to the 100th vote and the 101st push returns an error. Why Vec without bounds is a real bug no bounds (broken) #[account] pub struct Proposal { pub votes: Vec<Pubkey>, } at init time: space = 8 + ??? Anchor doesn't know. two ways this breaks: 1. you guess too low → push() overflows the account, tx reverts 2. you guess too high → user paid rent for bytes nobody uses either way, the size is wrong bounded with max_len #[account] #[derive(InitSpace)] pub struct Proposal { #[max_len(100)] pub votes: Vec<Pubkey>, } at init time: space = 8 + 4 + 10032 = 3,212 bytes predictable, fixed at init runtime behavior: push 101st vote → handler returns an error push 100th vote → succeeds, room runs out bounded, predictable, paid for once If you can't bound it, you can't put it on an account. Move overflow data to a separate PDA per item.

The bound is a design decision. Set it too low and your application breaks when users hit the cap. Set it too high and every account pays rent for bytes that may never be used. A 100-pubkey vote list at 32 bytes each is 3,200 bytes for the vector alone, which is enough to cost the user about 0.022 SOL in rent. That cost is paid up front, once, when the account is initialized. The user pays for a 100-vote bound whether they end up using 5 votes or 100.

Most production programs avoid putting variable-length data on a single account when the bound would be large or unbounded. Instead, each element gets its own PDA, derived from the parent account's key and an index or sub-identifier. A proposal with thousands of votes wouldn't store them in a Vec on the proposal account. It would store each vote in a separate Vote PDA derived from (proposal, voter). The proposal account stays small and predictable, and there's no architectural cap on how many votes can be cast.

The layout of existing accounts is hard to change

You can deploy a new version of your program that interprets the existing bytes differently, but the accounts created by the old version keep their size. The realloc constraint lets you change the size of a specific account in a specific instruction, paying or refunding rent as you go, but it doesn't help you migrate every existing account at once. Moving all of them to a new layout takes a new account type and a migration instruction that copies each old account into a new one and closes the old account, once per account.

So in practice you get one chance to pick the layout. Reserve a few bytes for future use if you think the struct might grow, and be conservative with Vec and String bounds. For each field, ask how big it needs to be and what happens if you are wrong.