Where methods live? Stack or in Heap?

前端 未结 6 427
攒了一身酷
攒了一身酷 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 14:08

    Each thread is allocated its own stack.

    This article has a good introduction to the memory separation within a Java process.

    Inside the Java virtual machine, each thread is awarded a Java stack, which contains data no other thread can access, including the local variables, parameters, and return values of each method the thread has invoked. The data on the stack is limited to primitive types and object references. In the JVM, it is not possible to place the image of an actual object on the stack. All objects reside on the heap.

    I've seen many scenarios where clients have implemented hugely threaded servers on the basis that each thread does very little, and they run into problems with memory. That's because each thread is allocated its own stack, and this (obviously) adds up. I think the default value is 512k per thread, but I've not found a canonical source for that.

提交回复
热议问题