Where methods live? Stack or in Heap?

前端 未结 6 424
攒了一身酷
攒了一身酷 2020-12-14 13:16

I know that local variables and paramters of methods live in stack, but I not able to figure out where does actually methods live in case of Java?

If I declare any T

6条回答
  •  春和景丽
    2020-12-14 13:58

    If I remember correctly, the method code itself will live in the code portion of memory, while the variables declared internally will live in the stack, and the objects will be created on the heap. In Java, the variable pointers and primitives live on the stack, while any created objects live in the heap.

    For a (poor) ASCII representation:

    -------
    |STACK|
    -------
    |FREE |
    -------
    |HEAP |
    -------
    |CODE |
    -------
    

    Where the STACK represents the stack, FREE represents free memory, HEAP represents the heap, and CODE represents the code space.

    This is what my memory says - some of the details might be wrong.

提交回复
热议问题