How does Java manager the memory?

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).

在这里输入图像描述

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:

  • Divide into 2 parts: Heap & Non-heap memory.
  • 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.


    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.


    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

    链接地址: http://www.djcxy.com/p/82868.html

    上一篇: 有关Java中本地最终变量的问题

    下一篇: Java如何管理内存?