From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Memory management and garbage collection

Memory management and garbage collection - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Memory management and garbage collection

- Even though Java is a compiled and statically typed language, it manages memory for you, unlike languages like C, C++ or Pascal. You don't have to explicitly allocate and deallocate memory whenever you create an object. The Garbage Collector is a major feature of the Java virtual machine that makes a lot of this possible. I'm going to describe how the Garbage Collector works and how little attention you as the programmer need to give it. When you create a variable that references a complex object, that variable is stored in the fastest available memory but the object it references is stored in heap memory. There are two kinds of memory, stack and heap. The stack memory is somewhat faster, heap a little bit slower. But heap is more dynamic. When you create primitive variables, depending on their context, they might be stored in stack or heap. But complex objects are always stored in heap. And as long as any variable references an object in memory, that object will be retained, it…

Contents