β 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.
- Ana starts a job. She puts its job card on her own workbench, where nobody else will touch it.
- Her job needs a chair, but nobody has made one yet. First, the chair's blueprint goes into the cabinet.
- Now Ana makes the chair. It goes into the shared storeroom, and her card only notes where it sits.
- Ben works at his own bench, with his own cards, but he can walk to the storeroom and use the very same chair.
- Benches are private, the storeroom is shared, and the cabinet keeps one blueprint per kind of piece.
- Ana starts the job and puts its card on her bench, with the notes she needs.
- First she must measure something, so a new card goes on top. Only the top card is being worked on.
- The measuring is done. The answer goes down to the card underneath.
- The finished card is thrown away at once, and Ana carries on with the job underneath. Nobody needs to tidy up.
- Next time, a mistake makes the measuring card ask for another measuring card, again and again. The pile grows.
- The bench is full and the pile falls over. Ana has to drop the whole job.
- Ana and Ben send their new pieces to the storeroom. They all land on the arrivals shelf.
- The tidier checks the shelf. Almost every piece is an offcut nobody holds a note for any more.
- The one piece still wanted moves to the long-term racks at the back.
- But a list on the wall keeps a note for every piece ever made. The racks fill with pieces nobody can throw away.
- The tidier finds nothing to clear, and Ben's next piece has nowhere to go. Work stops.
- Binder v1 brings the blueprints for chairs and tables, and the workshop files them in the cabinet.
- A chair made from those blueprints goes to the storeroom, and the list on the wall keeps a note of it.
- The workshop switches to new designs. A new binder arrives with its own copies of every blueprint, and v1 is retired.
- 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.
- Every switch leaves another binder behind, until the cabinet has no room for the next one.
- The workshop checks the size of its rented unit and, by default, gives the storeroom a quarter of the floor.
- The rest of the floor isn't empty. The cabinet, every bench, the template rack and the loading dock all take room.
- 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.
- Next time the storeroom gets three quarters of the floor, leaving space for everything else. It all fits.
- The manager asks the running workshop how many pieces of each kind are in the storeroom. Chairs top the count.
- 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.
- At the desk, the manager follows the notes backwards from the pile of chairs and finds the list on the wall holding them.
- 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.