Pandey Amit
Pandey Amit

Reputation: 703

Java Garbage Collector for stack

Is garbage collector responsible for stack removal?

Does it also deallocate memory for static/constant variables?

example:

class A
{
   void fun(){
   int x = 100;
   static int y = 200;
   final int z = 300;
   }
}

Is all three variable's memory deallocated when function gets completed?

Upvotes: 0

Views: 50

Answers (3)

sebo348
sebo348

Reputation: 21

Static fields will not be eligible for garbage collection as long as the class they live in is loaded. And according to Oracle docs:

A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector.

Upvotes: 2

redge
redge

Reputation: 1192

Basically garbage collection deallocates memory for all objects that are no longer referenced by any other object in the JVM. So it doesn't matter if it is a static or instance field or a local variable.

Upvotes: 0

SubOptimal
SubOptimal

Reputation: 22963

The garbage collector is responsible for orphan objects on the heap.

Have a look at this Oracle tutorial Java Garbage Collection Basics. It explain the GC basics.

Upvotes: 2

Related Questions