2

I'm trying to discover how Java manages memory.

I'm learning which part of memory holds static methods and static variables. They said PermGen memory holds static variables and static methods, which is a part of non-heap memory(picture below).

enter image description here

But, from what I've learnt, Java has stack segment to hold local variables, parameters, references, values of non-void function returns... etc, heap segment holds objects and arrays. I've read many many Q&A about this topic and have 2 ways to explain it:

  1. Divide into 2 parts: Heap & Non-heap memory.
  2. Divide into 3 parts: Stack segment, Heap segment, Code segment.

Who can help me to understand this more clearly. If memory is divided into 2 parts: Where's stack ? If memory is divided into 3 parts: Which part holds static methods and static variables ?

I would be grateful to get your help :D Thanks.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Thân Hoàng
  • 166
  • 1
  • 8
  • 1
    There are many, many ways to describe the way that Java does (or used to) manage memory. Many are more or less correct. Your question is too broad. – Stephen C Mar 09 '18 at 10:28
  • 1
    See https://stackoverflow.com/questions/1262328 – Stephen C Mar 09 '18 at 10:31
  • Please read the following manual from Oracle http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf – Victor Gubin Mar 09 '18 at 10:33

2 Answers2

0

Bottom line, there are two parts:

  • Stack: Used for local variables and controlling program execution
  • Heap: Used for all objects, including instances of Class class, that contain static variables.

In the stack, the object typed variables, contain references to objects stored in the heap.

Andres
  • 10,561
  • 4
  • 45
  • 63
0

Stack is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method.

Heap memory is used to allocate memory to objects and java runtime environment classes.

More

shb
  • 5,957
  • 2
  • 15
  • 32
  • So, can you tell me which memory contain static method and static variable ? I've read one question same, they said It is contained by PermGen memory(Class Area). Please tell me if that question is wrong. https://stackoverflow.com/questions/6569557/what-is-the-actual-memory-place-for-static-variables – Thân Hoàng Mar 09 '18 at 10:38
  • Its the heap. Classes and all not instance data is stored in the Permanent Generation section of the heap. Check the link for more details. https://stackoverflow.com/questions/3849634/static-allocation-in-java-heap-stack-and-permanent-generation/3849819#3849819 – shb Mar 09 '18 at 10:42
  • Permgen was removed in Java 8: https://stackoverflow.com/questions/18339707/permgen-elimination-in-jdk-8. We are now on Java 9. – Stephen C Mar 09 '18 at 13:14