方法在哪里生活?堆还是堆? [英] Where methods live? Stack or in Heap?

查看:90
本文介绍了方法在哪里生活?堆还是堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道方法的局部变量和参数在堆栈中,但是我无法弄清楚在Java中方法实际在哪里?

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?

如果我声明任何Thread对象,如:

If I declare any Thread object like:

Thread t=new Thread();
t.start();

因此,这意味着除了主方法之外,我还创建了一个单独的方法调用.这是什么意思?这是否意味着在堆栈内存上调用单独的方法序列?我说的对吗?

So it means I've created a separate calling of methods apart from main method. What does it mean? Does it mean calling of separate sequence of methods over Stack memory? Am I right?

推荐答案

每个线程都分配有自己的堆栈.

Each thread is allocated its own stack.

本文有很好的介绍Java进程中的内存分离.

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

在Java虚拟机内部,每个 线程被授予Java堆栈,该堆栈 包含其他线程无法处理的数据 访问,包括局部变量, 参数,以及每个参数的返回值 线程已调用的方法.这 堆栈上的数据仅限于 基本类型和对象引用. 在JVM中,不可能 将实际物体的图像放在 堆栈.所有对象都驻留在 堆.

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.

我已经看到许多场景,其中客户端基于每个线程只做很少的事情实现了线程数量巨大的服务器,并且它们遇到了内存问题.那是因为每个线程都分配有自己的堆栈,并且(显然)加起来了.我认为的默认值为每个线程512k,但是我没有找到规范的来源.

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.

这篇关于方法在哪里生活?堆还是堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆