Flyweight: sharing to save memory

  • Lesson 17 of 32
  • 10 min
  • Beginner
  • 12 steps

A game draws a forest and every tree loads its own texture. You will split what repeats from what varies, share it through a factory and see what it costs you.

Copies of the species data
3
10,000 treestreeType()Integer.valueOftreeType()oakInteger127mesh +materialoakpinepinebirchbirchtexture copied in every treeEvery tree with everythingclass Tree {▸species = "oak";▸texture = load("oak.png");constructor(public x: number,public y: number) {}}2 of 4 fields repeatA tree's fieldsspecies"oak"intrinsictextureoak.pngintrinsicx4extrinsicy0extrinsicshared by thousandsx, y + a referenceTwo typestype TreeType = Readonly<{species: string; texture: Image }>;type Tree = {x: number; y: number;type: TreeType };treeType("oak")exists: the same objectThe factory with a cachefunction treeType(species: string) {▸let t = cache.get(species);if (!t) {t = Object.freeze(load(species));cache.set(species, t);}▸return t;}treeType("birch")new: stored onceIf missing, created oncefunction treeType(species: string) {let t = cache.get(species);▸if (!t) {▸t = Object.freeze(load(species));▸cache.set(species, t);}return t;}x, y: per treetexture: sharedJoined when drawingfor (const t of forest) {▸draw(t.type.texture, t.x, t.y);}// shared texture, own positionimmutableOne change for allconst oak = treeType("oak");▸oak.texture = autumn;// strict mode: TypeError// the object is frozena cache of parts, not resultsThree misconceptions"it is any cache"shares immutable parts"it always saves memory"costs more with few objects"== compares values"only with cached objectsa == b: the same object128: no guaranteeThe Integer cacheInteger a = Integer.valueOf(127);Integer b = Integer.valueOf(127);a == b; // trueInteger c = Integer.valueOf(128);Integer d = Integer.valueOf(128);▸c == d; // may be falsec.equals(d); // truecolour and scale per instanceWhere you meet itJava String.intern()one object per equal textPython sys.interncompares keys by pointerUnity GPU instancingone mesh, many instancesgrows without bound?Do not use it if…thousands, not millionsthe saving does not paydata differs per objectnothing to shareyou did not measure memoryyou do not know you winthe cache has no limitit grows foreverone object per speciesFlyweight in a tableproblemthousands of equal copiessolutionshare the part that repeatswho hands it outa factory with a cacherulewhat is shared is immutabledo not use itwithout measuring memory
  • request
  • response
  • failed request

Step 1 of 12

Problem

Ten thousand trees, ten thousand textures

A game draws a forest. Each Tree stores its species, its texture and its position. With 10,000 trees (example value) there are 10,000 full objects.

But there are only two species. An oak's texture is the same in every oak, and still it is loaded once per tree. Memory goes into copying the same thing again and again.

Step 2 of 12

Concept

What repeats and what does not

Look at a tree's four fields. species and texture hold the same value in thousands of trees: that is the intrinsic state, the part that can be shared.

x and y change in every tree: that is the extrinsic state, the per-use part. Flyweight starts with this split; without it there is nothing to share.

What changed

  • species, texture: shared
  • x, y: per tree

Step 3 of 12

Solution

Two objects for the whole forest

We move the intrinsic part into its own type, TreeType, and create one object per species: one for the oak and one for the pine.

Each Tree keeps x, y and a reference to its TreeType. The 10,000 trees point to only 2 species objects. That shared object is the flyweight.

What changed

  • TreeType (2 objects)
  • 9,998 copies of species and texture

Step 4 of 12

Technique

A factory hands out the shared objects

Who makes sure a second oak never appears? A factory with a cache: treeType(species) looks in a Map and, if the species already exists, returns the same object.

The forest asks for treeType("oak") and gets the oak that was already there. Nothing new is created. No other code creates a TreeType: only the factory does.

Step 5 of 12

Technique

A new species is created only once

Now the forest asks for treeType("birch"). The Map has no birch, so the factory creates it, freezes it with Object.freeze and stores it.

The next time someone asks for "birch" they get that same object. There are 3 species objects, even if the forest holds thousands of birches.

What changed

  • birch in the cache

Step 6 of 12

Technique

What varies travels separately

To draw, the code joins both parts on the spot: the texture comes from the shared object and the position comes from each tree.

The shared object does not know where it is: it has no x or y. If it had them, it could not serve two trees at once. That is the price: extrinsic state has to be passed around.

Step 7 of 12

Trade-off

Shared means immutable

Imagine someone changes one oak's texture to paint it for autumn. Because the object is shared, all 10,000 oaks would change at once.

That is why the factory freezes it. With Object.freeze, the write changes nothing, and in strict mode (JavaScript modules are strict) it throws a TypeError. If one tree needs something different, that is extrinsic state and it belongs in the tree.

Step 8 of 12

Concept

It is not just any cache

Three common misconceptions. Flyweight is not "any cache": a cache stores results; a flyweight is an immutable part that many objects share.

It does not always save memory: with few objects the Map and the indirection cost more. And == does not compare values: it only gives true when both sides came from the same cache.

Step 9 of 12

Technique

Where you meet it: `Integer.valueOf`

The Java documentation says Integer.valueOf(int) always caches values from -128 to 127, and may cache others. Asking for 127 twice returns the same object, so a == b is true.

With 128 the cache is not guaranteed: c == d may be false. It is a classic interview question and a real bug. To compare values, use equals.

Step 10 of 12

Technique

Interned strings and instancing

Java keeps a pool of strings: s.intern() == t.intern() is true exactly when s.equals(t). In Python, sys.intern turns comparing dictionary keys into comparing pointers.

In games, Unity's GPU instancing draws many copies of the same mesh with the same material in a single draw call, and each instance has its own properties, such as colour or scale. Same split: shared mesh, per-instance position.

Step 11 of 12

Trade-off

When Flyweight does not pay off

If you have thousands of objects rather than millions, or the "shared" data actually differs per object, there is nothing to gain: the Map, the factory and the state you pass from outside are pure complexity.

And the cache can grow without bound: pick a fixed set of keys or a policy to evict entries. Even Unity warns that instancing can cost more than it saves depending on the platform. Before applying it, measure memory.

Step 12 of 12

Summary

Store once what repeats

When you need a huge number of similar objects, split the part that repeats (intrinsic) from the part that varies (extrinsic). The first lives once, immutable, handed out by a factory with a cache.

Each use keeps only its own data and a reference. Compare with equals, not ==, and do not use it without measuring.

You will learn

  • Split an object's data into the shared part (intrinsic) and the per-use part (extrinsic).
  • Share the intrinsic part through a factory that keeps the objects in a cache.
  • Explain why a shared object has to be immutable.
  • Recognise Flyweight in the `Integer` cache, in string interning and in game engine instancing.
  • Know when Flyweight does not pay off.