Rips
Rips

Reputation: 2004

Heap,Non heap and stack ..intricacies of garbage collection

I have been going through the garbage collection in java(jdk 6 hot spot JVM).I have few questions which I hope the community will help me to resolve.

What I understand:

1)Heap is divided into

a)Young generation -Eden and Survivor :New objects and arrays are created into the young generation.Minor garbage collection will operate in the young generation. Objects, that are still alive, will be moved from the eden space to the survivor space.

b)Old generation/Tenured Generation:Major collection will move the still alive objects from young generation to old generation.

2)Non Heap is divided into

    a)Code Cache
    b)Perm generation.

What I want to know:

1)what if survivor gets full..how will minor garbage collection work.

2)When and how is the perm generation garbage collected.
3)Also what happens to the stack..where is it stored or residing?How is its size controlled?

Upvotes: 4

Views: 1916

Answers (3)

Rips
Rips

Reputation: 2004

A stack is the part of the memory. The local automatic variable is created on this stack and method arguments are passed. When a process starts, it get a default stack size which is fixed for each process. In today's operating system, generally, the default stack size is 1 Mb, which is enough for most of the process. Under abnormal condition, the stack limit exceeds. This is known as stack overflow.

Upvotes: 0

Gabe
Gabe

Reputation: 86708

The stack size is controlled by being fixed at the point that it is created. If you ever try to use more space than is available on the stack, you will get a "stack overflow" exception.

Upvotes: 0

andrewdotn
andrewdotn

Reputation: 34813

  1. When the survivor space is full, objects are moved into the old generation. Although, technically, most of the time when an object gets moved from the survivor space into the old generation, it’s not because the survivor space is full, but because the object has survived a certain number of minor collections, usually 10–15.
  2. Very rarely. It’s mostly the binary code for Java classes, so space can only be freed up if a bunch of classes are unloaded from memory. Most programs use the same set of classes throughout the life of the program, so collecting the permanent generation is generally a waste of time. Basically Java will only do a collection here if it’s about to run out of memory.
  3. The stack is something outside the heap, and its size is controlled by the fact that objects are only stored on the stack if they are guaranteed to have a limited lifetime. These are mostly local variables. Suppose you have a local StringBuilder variable that you use to build up the return value of a method. You never pass it outside your own method, and you call stringBuilder().toString() to create a new object at the end of the method. Since Java can tell that the StringBuilder object won’t outlive the running of the method, it can put it on the stack and deallocate it immediately when the method returns instead of passing it off to the garbage collector.

Upvotes: 1

Related Questions