java和内存布局
嘿伙计们,我正在复习一些问题,但我真的搞不懂,我浏览了课本,但我不确定在哪里可以找到答案......
我知道做内存图会很困难没有图片,但请耐心等待。
interface Lovable
public void love();
class Foo implements Lovable
public void love();
// something
public int val()
// return something1
public class Love
public static void main(String args [])
Foo foo = new Foo()
foo.love()
foo.love()
int bar = =foo.val()
System.out.print(v)
现在,我看到 foo 是用 new
声明的,所以我知道实际的 Foo 类信息存储在堆中,并且有一个框架?指针?指向堆栈顶部堆中的内存空间(在 foo 调用任何方法之前)。那么界面呢?那也会存储在堆中吗?
所以堆栈底部将是 Love 类(也包含 int bar),一个指向堆中 Foo foo 的指针,一个 foo.love() 的框架,另一个框架 foo.love(),一个 foo 的 fram .val(),用于打印的框架?
我明白了吗?或者我真的离得很远吗?如果您知道我可以在哪里获得更多信息,请告诉我。我很感激任何意见..
Hey guys, I'm reviewing some questions but I can't really figure it out, i looked through the text book but i'm not sure where i can find answer...
I know it would be quite hard to do memory diagrams w/o pictures but please bear with me.
interface Lovable
public void love();
class Foo implements Lovable
public void love();
// something
public int val()
// return something1
public class Love
public static void main(String args [])
Foo foo = new Foo()
foo.love()
foo.love()
int bar = =foo.val()
System.out.print(v)
Now, I see that foo is declared with new
, so I know actual Foo class information is stored in heap and there's a frame?pointer? that points to that memory space in heap on top of the stack (before foo calls any methods). so then what about the interface? would that be stored in the heap too?
so on the bottom of the stack would be the class Love(also contains int bar), a pointer that points to Foo foo in heap, a frame for foo.love(), another frame foo.love(), a fram for foo.val(), a framee for print?
Am i getting the idea ? or am i really really far off? If you know any where I can get more information, please let me know. I appreciate any input..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,对象存储在 堆上 由垃圾收集器管理。
仅最新版本的 Java 6 具有逃逸分析功能,如果对象没有逃逸,则将其存储在堆栈上。
类信息存储在权限空间中。
Generally, are objects are stored on the heap managed by the garbage collector.
Only the latest release of Java 6 has escape analysis to store objects on the stack if they do not escape.
Class information is store in the perm space.
内存布局取决于 JVM,并且 JVM 在如何使用内存方面有很大的余地,只要它们保持程序员所考虑的 Java 对象模型的逻辑视图即可。 Sun JVM 有多个“堆”,因为它实现了分代垃圾收集。对象是在eden空间中创建的,eden空间被视为堆栈,因此可以非常快速地创建对象。如果它们的寿命足够长,对象就会被转移到寿命较长的世代,并且这些对象的实现方式更像正常的动态分配堆。 JVM 将类和内部字符串存储在“permspace”堆中。永久空间实际上并不是永久的:当不再有对其实例或类加载器的引用时,类就会被收集。并且,如上所述,如果 Java 6 可以确定对对象的引用不会离开块,则它会在调用堆栈上分配一个对象。
Memory layout depends on the JVM and JVMs have lots of leeway about how to use memory as long as they maintain the logical view of the Java object model that the programmer thinks about. The Sun JVM has several "heaps" because it implements generational garbage collection. Objects are created in the eden space, which is treated as a stack so that objects can be created very quickly. If they live long enough, objects get moved to longer lived generations, and those are implemented more like normal dynamically allocated heaps. The JVM stores classes and interned strings in the "permspace" heap. Permspace is not actually permanent: classes are collected when there are no more references to their instances or classloader. And, as pointed out above, Java 6 will allocate an object on the call stack if it can determine that references to the object do not leave the block.
foo
引用位于堆栈上。foo
指向的对象位于堆上(在简单情况下实际上可能会优化到堆栈上,但从概念上讲它位于堆上)。对象的类可以具有超类并实现接口。但是,无论在哪个类中声明字段,实例字段都保存在堆上相同的内存分配中。
foo
the reference is on the stack. The object pointed to byfoo
is on the heap (it may actually be optimised onto the stack in simple cases, but conceptually it is on the heap).The object's class may have superclasses and implement interfaces. However, whichever class a field is declared in, instances fields are all kept in the same allocation of memory on the heap.