LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java heap organization (https://www.linuxquestions.org/questions/programming-9/java-heap-organization-661491/)

Clemente 08-08-2008 04:40 PM

java heap organization
 
Hi all,

does anyone know, how java organizes its heap space?

Assume a class with some fields (data) and a lot of methods (code) in it. If I do instantiate this class multiple times, do I allcate space for the code multiple times, too? Or does java share the code for all classes of the same type?

Thanks a lot,
c-mens

ischi 08-08-2008 05:38 PM

In my Opinion the Code shouldn't be stored on the Heap, but in the Text/Code Segment. Isn't this the same across all languages? Of course the Variables and such have the be different for every instance but the Code itself stays the same, thats what the Text Segment is for.

I'm not a Java expert but that more of an OS Question ... and thats what Tannenbaum says for Storage organization, so I guess it should comply to Java, too.

Correct me if I'm mistake,

jlliagre 08-09-2008 02:25 AM

Classes and methods are stored in the Java Heap. They are loaded on demand and are obviously not stored more than once there at a given time. They are in a region called the permanent generation similar to the text segment for native programs. They are subject to garbage collection should memory demand occur for new classes to be loaded.

Clemente 08-10-2008 05:36 AM

Thanks a lot for your reply!
Just to get it correct:

1) Classes are loaded once (into the permanent generation section of java heap).
2) If I instantiate a class, that instance allocates memory for an pointer (or similar) to the class and it allocates memora for all non-static fields.

Is this roughly right?
C-mens

jlliagre 08-10-2008 09:53 AM

1: yes. Actually a class can be loaded more than once in the JVM lifetime given the fact it can be unloaded from memory.

2: Instance data is allocated on young generation space and either quickly subject to garbage collection or moved to longer living object space.


All times are GMT -5. The time now is 11:31 AM.