Java and memory layout

Hey guys, I am looking through some questions, but I cannot understand it, I have looked through a text book, but I am not sure where I can find the answer ...

I know that it would be rather difficult to make memory diagrams without photos, 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 using new , so I know that the actual information of the class Foo is stored in the heap and is there a pointer to the frame? which points to this memory space on the heap on top of the stack (before foo calls any methods). so what is the interface then? will it be stored on the heap too?

therefore, at the bottom of the stack there will be a Love class (also containing an int bar), a pointer pointing to Foo foo on the heap, a frame for foo.love (), another frame foo.love (), a frame for foo.val (), a frame for print?

Am I getting an idea? or am I really very far away? If you know where I can get more information, please let me know. I appreciate any input.

+1
source share
3 answers

Typically, objects are stored in a heap managed by the garbage collector.

Only the latest version of Java 6 has an escape analysis to store objects on the stack if they don't go away.

Class information is stored in perm space .

+4
source

The layout of the memory depends on the JVM and the JVM have many possibilities for using memory if they support the logical representation of the Java object model that the programmer is thinking about. The Sun JVM has several heaps because it implements a collection of garbage. Objects are created in the eden space, which is considered to be a stack, so objects can be created very quickly. If they live long enough, objects move to longer-lived generations, and they are implemented more like ordinary dynamically distributed heaps. The JVM stores classes and interned lines in the permspace heap. Perm space is not really permanent: classes are assembled when there are no more references to their instances or the class loader. And, as stated above, Java 6 will allocate an object in the call stack if it can determine that object references do not leave the block.

+1
source

foo link is on the stack. The object pointed to by foo is on the heap (in fact, it can be optimized on the stack in simple cases, but conceptually it is on the heap).

An object class can have superclasses and implement interfaces. However, no matter what class is declared in the field, all instance fields are stored in the same memory allocation on the heap.

0
source

Source: https://habr.com/ru/post/1412686/


All Articles