skip to content

In a mobile recipe feed thousands of items deep, why create views only for rows near the viewport and recycle them?

level: juniorimportance: must knowfreq 62%

answer

  1. cost should follow the screen
  2. records are cheap, laid-out views are not
  3. visible rows plus a small buffer
  4. a pool of views per row type
  5. binding sets or clears every field

basics

~20 s

A view per item makes memory and creation work grow with the feed, though the screen shows about five cards. Virtualization keeps views only near the viewport, and recycling rebinds scrolled-off views to incoming items, so cost tracks the screen.

solid answer

~50 s

A row view costs memory for its layout, text and image box, and creating and laying it out is some of the most expensive work a list does. With one view per item those costs grow with a feed that has no end, while the reader only ever sees a handful of cards. **Virtualization** keeps the items as lightweight records and holds views only for the visible rows plus a small buffer above and below. **Recycling** puts views that scroll out into a pool per view type and rebinds them to rows scrolling in, so a long scroll mostly binds instead of creating. The price is discipline: a recycled view still shows its previous item until binding overwrites it, so `bind` must set or clear every field, and state the user changed must live in the item's record or under its id, never only in the view.

go deeper

for a junior

Recall the two ideas separately: virtualization limits views to the visible rows plus a buffer, and recycling reuses those views for new rows instead of creating fresh ones.

for a middle

Explain the pool per view type and the bind step, and why binding must set or clear every field because a recycled view still carries its previous item.

for a senior

Show that live views follow the screen, size the buffer from stated scroll speed and bind time, and name the bugs recycling invites: leftover state and late size corrections.

for a principal

Weigh where virtualization stops paying — short or bounded lists — and set one rule a whole team can follow: a row is a function of its item's record and its per-id state.

## Why one view per item fails A **row view** is the on-screen object that draws one item of a list: its layout, its text, its image box and whatever memory the toolkit keeps for them. Building one for every recipe in a feed thousands of items deep means memory and creation work grow with the length of the feed, while what the reader can see stays at a handful of cards. On a phone that is the wrong scaling. The feed can grow without bound during one session; the device's memory and its time per frame do not. Creating and laying out a view is also among the most expensive things a list does, and doing it for rows nobody has scrolled to yet is pure waste. ## Virtualization: a small window of views over a long list of records **Virtualization** separates the data from the views. The feed's items live as lightweight **records** — in the local store and in the screen's state — while the list keeps views only for: - the rows currently in the **viewport**, the visible part of the list, and - a small **buffer** above and below it, so a row is ready a moment before it scrolls in. As the reader scrolls, rows leaving the buffer give up their views and rows entering it get one. The list still knows how many items exist and roughly how tall each is, so the scroll indicator and the offsets describe the whole feed even though most of it has never been drawn. ## Recycling: rebinding instead of re-creating Virtualization alone would still create a fresh view for every row entering the buffer and discard one for every row leaving it. **Recycling** keeps the departing views in a **pool** and hands one back when a new row needs a view of that kind. The steady state of a scroll becomes: 1. A row scrolls out past the buffer, and its view goes to the pool for its **view type**. 2. A new row enters the buffer, and the list takes a view of the matching type from that pool, creating one only when the pool is empty. 3. The view is **bound** to the new item: every field is set from that item's record. After the first few screens, a long scroll creates few new views; most of the work per row is binding. | approach | views alive | work while scrolling | main risk | |---|---|---|---| | one view per item | grows with the feed | large up front, then memory held | memory and start-up cost | | virtualized, no recycling | screen plus buffer | one creation per row scrolled in | churn on long scrolls | | virtualized with recycling | screen plus buffer, plus small pools | mostly binding | state left over from the previous item | ## A worked size Assume, as the scenario's own figures rather than any standard, a phone that shows about five recipe cards at once and a buffer of one screen above and one below. The list then holds roughly fifteen card views, plus a few spare in the pools, whether the reader is at item 30 or item 3,000. The records for 3,000 recipes — ids, titles, image references, a saved flag — are small next to 3,000 laid-out views. That difference is the whole point of the technique. ## What recycling costs you - **Binding must be complete.** A recycled view keeps showing its previous item until binding overwrites it, so every field is set or explicitly cleared: a recipe with no `sponsored` label must clear the label the previous card carried. - **Pools are kept per view type.** A large recipe card, a small card, a section header and a sponsored card have different layouts; a view is reused only for a row of its own type, otherwise binding would have to rebuild its structure. - **Rows not yet built still need a size.** Offsets above and below the viewport come from known or estimated sizes, and an estimate corrected late is one cause of a list that jumps. - **State must not live only in the view.** Anything the user changed on a row belongs in the item's record or in state keyed by the item's id, or it reappears on whichever item the view shows next. ## Where this sits in the design Virtualization governs views, not data. Loading the next page of items from the network is a separate mechanism with its own trigger, and how a given toolkit exposes its buffer size or its pools is that toolkit's own subject. Some list components reuse view objects; others re-create a row's content from a declaration each time it enters the window. The design rule is the same in both: what a row shows is a function of its item, and the number of live views follows the screen, not the length of the feed.

  • How large should the buffer around the viewport be?
    Large enough that a row is bound before it appears at the reader's normal scroll speed, small enough that memory and binding work stay bounded. Derive it from the scenario — rows per screen, binding time, typical scroll speed — and label those as assumptions; no figure is a standard. A larger buffer smooths fast scrolls at the cost of more live views and more binding of rows that may never be seen.
  • Does virtualization pay off if a list only ever holds 30 short rows?
    Much less. Building 30 short rows is a small, bounded cost, while virtualization brings size estimates and recycled-state bugs to guard against. It pays when the list is long or unbounded, or its rows are expensive to build — an infinite recipe feed is exactly that case; a settings screen usually is not.

A restaurant with fifteen tables can serve a thousand guests in an evening because tables are turned, not built: when one party leaves, the table goes back into service for the next. Turning a table means clearing every plate the last party left, or the next party sits down to someone else's dinner.

  • Virtualization means asking the server for fewer items.
  • A recycled view comes back blank, so binding only sets fields that changed.
  • A header's view can be reused for a recipe card, since both are rows.
  • Off-screen views cost nothing because they are not drawn.
  • A virtualized list needs no size for rows it has not built yet.