Anchor init of a 10,000-element Vec fails: one instruction can only alloc 10240 bytes, realloc repeatedly
Symptom: an Anchor account declared with a large #[max_len(...)] vector, e.g.
#[account]
#[derive(InitSpace)]
pub struct Data { #[max_len(10000)] pub indexes: Vec<UserData> }fails to initialise. Depending on how you invoke it you see the transaction exceed limits, or the init instruction fails outright, when space = 8 + Data::INIT_SPACE is passed.
Cause: the Solana runtime caps how much account space a single instruction can allocate or reallocate at **10240 bytes** (MAX_PERMITTED_DATA_INCREASE). A struct holding 10,000 variable-length records is far beyond that, so init with the full size can never succeed no matter how much rent you fund.
Fix: allocate incrementally. Initialise the account with a size under 10240 bytes, then send additional instructions that each realloc the account by at most 10240 more bytes (paying the extra rent each time) until it reaches the target size, tracking progress in the account itself so the growth is resumable. In Anchor this is the #[account(mut, realloc = ..., realloc::payer = ..., realloc::zero = false)] constraint applied repeatedly. Total account size is separately capped at 10 MiB.
Better design in most cases: stop storing an unbounded collection in one account. Give each record its own PDA keyed by index or owner, and iterate client-side with getProgramAccounts and a memcmp filter. That removes the growth problem entirely and avoids deserialising a huge vector (which also costs compute units and heap on every instruction that touches it).
Non-obvious part: InitSpace happily computes a size the runtime will never allocate, so the failure appears at runtime rather than at build time.
Source: adapted from a Stack Exchange answer by McBain (question by GoT), https://solana.stackexchange.com/questions/15183/handle-big-vector-containing-ton-of-structs — CC BY-SA 4.0