How JVM and Java Memory Management Works — Summary

Nil Seri
2 min readJan 22, 2022

Java Virtual Machine Architecture and Java Memory Management Summary

Photo by simon peel on Unsplash

What is JVM?

  • JVM (Java Virtual Machine) is a part of JRE (Java Runtime Environment).
    JRE is a set of components to create and run a Java application.
  • Java code is compiled into Java byte code.
    JVM converts Java byte code into machine language.
  • JVM provides a runtime environment for Java code / apps (an instance).
  • JVM is also responsible for allocating memory space.
JVM Architecture Diagram — https://www.guru99.com/java-virtual-machine-jvm.html

JIT (Just In Time Compiler)

  • compiles parts of byte code with similar functionality to reduce time for compilation
  • compiler here ->JVM’s instructions->a specific CPU’s instruction

JNI (Java Native Interface)

  • framework provides interface to communicate with another app written in another language (C, C++, Assembly, etc.)
  • Java uses JNI to send output to the console or interact with OS libs.

Java Memory Management

Method Area
- a fully qualified name of a type (for ex. String)
- the type’s modifiers
- type’s direct superclass name
- type’s super interfaces

Heap Area
- actual objects (new keyword) -> their references are at stack
- heap becomes full -> garbage collector (gc)

Reference Types
- Strong Reference (not eligible)
- Weak Reference (when gc runs)
- Soft Reference (when low on memory)
- Phantom Reference (whenever gc wants)

Mark and Sweep Algorithm
- root object
- heap is traversed, find gap between live objects
- non-reachable objects are collected.

https://www.javatpoint.com/memory-management-in-java

Happy Coding!

--

--

Nil Seri

I would love to change the world, but they won’t give me the source code | coding 👩🏻‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨