TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About Β· Labs Β· Cheat sheets Β· Learning paths

← Back to JVM memory: heap, stack and metaspace

πŸ› οΈ JVM memory: heap, stack and metaspace: cheat sheet

🎨 The analogyA workshop: each worker stacks their job at their own bench, and finished pieces go to one shared storeroom.
πŸŽ“ The regular explanationWhere a running Java program keeps things. Each thread has its own stack of frames for local variables, objects live on a shared garbage-collected heap, class metadata lives in native metaspace, and the whole process must fit its container's memory limit.
Stacks, heap and metaspaceStep 5 of 5
points topoints toAna's workbench Β· privatemaikey main(): frame: main0123Ben's workbench Β· privaterunkey run(): frame: run0123Storeroom Β· sharedo1key Order object: o10123Blueprint cabinet Β· one per classOrdkey class Order: metadata: Order0123πŸ‘©β€πŸ”§AnaπŸ‘¨β€πŸ”§Ben
  1. Ana starts a job. She puts its job card on her own workbench, where nobody else will touch it.
  2. Her job needs a chair, but nobody has made one yet. First, the chair's blueprint goes into the cabinet.
  3. Now Ana makes the chair. It goes into the shared storeroom, and her card only notes where it sits.
  4. Ben works at his own bench, with his own cards, but he can walk to the storeroom and use the very same chair.
  5. Benches are private, the storeroom is shared, and the cabinet keeps one blueprint per kind of piece.
Frames on a thread stackStep 6 of 6
πŸͺš Ana's workbenchπŸ‘©β€πŸ”§AnaπŸƒ Card: build orderqty=3 size=40πŸƒ Card: measureπŸƒ Card: measureπŸƒ Card: measureStackOverflowError
  1. Ana starts the job and puts its card on her bench, with the notes she needs.
  2. First she must measure something, so a new card goes on top. Only the top card is being worked on.
  3. The measuring is done. The answer goes down to the card underneath.
  4. The finished card is thrown away at once, and Ana carries on with the job underneath. Nobody needs to tidy up.
  5. Next time, a mistake makes the measuring card ask for another measuring card, again and again. The pile grows.
  6. The bench is full and the pile falls over. Ana has to drop the whole job.
The heap fills upStep 5 of 5
πŸ“¦ StoreroomArrivals shelf Β· cleareds1key session object: s10t1key temp object: t11t2key temp object: t22t3key temp object: t334Long-term racks Β· fulls1key session object: s10c1key cached entry: c11c2key cached entry: c22c3key cached entry: c33c4key cached entry: c44πŸ‘©β€πŸ”§AnaπŸ‘¨β€πŸ”§BenOutOfMemoryError🧹Tidier
  1. Ana and Ben send their new pieces to the storeroom. They all land on the arrivals shelf.
  2. The tidier checks the shelf. Almost every piece is an offcut nobody holds a note for any more.
  3. The one piece still wanted moves to the long-term racks at the back.
  4. But a list on the wall keeps a note for every piece ever made. The racks fill with pieces nobody can throw away.
  5. The tidier finds nothing to clear, and Ben's next piece has nowhere to go. Work stops.
A class loader leakStep 5 of 5
holdskeeps aliveBlueprint cabinet Β· fullv1Okey Order (loader v1): v1O0v1Ckey Cart (loader v1): v1C1v2Okey Order (loader v2): v2O2v2Ckey Cart (loader v2): v2C3v3Okey Order (loader v3): v3O✦✦✦✦✦4v3Ckey Cart (loader v3): v3C✦✦✦✦✦5Storeroomo1key Order from loader v1: o1012πŸ“˜ Binder v1retiredπŸ“— New bindersπŸ“ List on the wall
  1. Binder v1 brings the blueprints for chairs and tables, and the workshop files them in the cabinet.
  2. A chair made from those blueprints goes to the storeroom, and the list on the wall keeps a note of it.
  3. The workshop switches to new designs. A new binder arrives with its own copies of every blueprint, and v1 is retired.
  4. But that one old chair is still on the list, and it points back to binder v1. So the whole old binder has to stay.
  5. Every switch leaves another binder behind, until the cabinet has no room for the next one.
Fitting in a containerStep 4 of 4
🏭 Rented unitπŸ“¦Storeroom75% of limitπŸ—„οΈBlueprint cabinetπŸͺšWorkbenches1 MiB per threadπŸ“Template rack🚚Loading dockNIO buffersπŸ§‘β€πŸ’ΌLandlord
  1. The workshop checks the size of its rented unit and, by default, gives the storeroom a quarter of the floor.
  2. The rest of the floor isn't empty. The cabinet, every bench, the template rack and the loading dock all take room.
  3. Someone makes the storeroom as big as the whole unit. Once everything together spills over the line, the landlord shuts the workshop on the spot.
  4. Next time the storeroom gets three quarters of the floor, leaving space for everything else. It all fits.
Diagnosing with jcmdStep 4 of 4
πŸ§‘β€πŸ’ΌManager🏭WorkshopOrder: 2MπŸ“Έ Storeroom photoa list holds themπŸ—ΊοΈ Floor planThread: +600 MiB
  1. The manager asks the running workshop how many pieces of each kind are in the storeroom. Chairs top the count.
  2. To find out why, the manager has everyone stop for a moment and takes a photo of the whole storeroom, with every note pointing at every piece.
  3. At the desk, the manager follows the notes backwards from the pile of chairs and finds the list on the wall holding them.
  4. Another day the unit is full but the storeroom isn't. The manager measures every area on the floor plan and sees the benches grew.

🧩 Key questions

Ana's card says "the chair on shelf 12". Where is the chair itself?
In the shared storeroom; the card only says where it is. Cards hold small notes and where-to-find-it notes. The pieces themselves go to the storeroom, which every worker can reach.
A task keeps asking for the same task again, forever. What goes wrong?
Ana's pile of cards grows until it falls off her bench. Every new task puts another card on top, and a bench has a fixed size. A bigger bench only delays the fall.
The workshop keeps switching to new designs, and the cabinet eventually overflows. Why?
An old piece is still kept somewhere, so its whole old binder can't be thrown out. One kept chair holds on to its binder, and the binder holds every blueprint in it. Each switch leaves another binder behind.
The unit is 1,000 square metres and the storeroom is set to 1,000 too. What happens?
Benches, the cabinet and the dock spill over the line, and the landlord shuts the workshop. The storeroom isn't the only thing on the floor. Give it a share, such as three quarters, and leave room for the rest.