方法存储在内存中的什么位置? [英] Where are methods stored in memory?

查看:22
本文介绍了方法存储在内存中的什么位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到类字段存储在堆中,但是方法存储在哪里?在堆中还是在其他地方?它们是内联的吗?

I learned that class fields are stored in the heap, but where are methods stored? In the heap or somewhere else? are they inline?

推荐答案

方法存储在内存中的其他地方.请注意,方法是针对每个类的,而不是针对每个实例的.因此,通常情况下,方法的数量不会随着程序的运行时间而改变(也有例外).在传统模型中,方法所在的地方称为代码段".在 .net 中,这更困难:方法最初存在于程序集中,并被映射到进程内存中.在那里,即时编译器在本机代码中创建一些方法的第二个副本;这个副本被执行.JIT 代码可能会在运行时多次创建和删除,因此将其视为在堆中"也是可行的.

Methods are stored somewhere else in the memory. Notice that methods are per-class, not per-instance. So typically, the number of methods doesn't change over the run-time of a program (there are exceptions). In traditional models, the place where the methods live is called the "code segment". In .net, it's more difficult: the methods originally live in the assembly, and get mapped into the process memory. There, the just-in-time compiler creates a second copy of some methods in native code; this copy gets executed. The JIT code may get created and deleted several times during the runtime, so it is practical to view it also as living "in Heap".

这篇关于方法存储在内存中的什么位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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