+ 7

Java and memory

I am confused about memory stuff. As i know there are 4 areas in the memory which is : text, static/global,stack, heap. But all articles what i found on java is talking just about the stack and the heap. What about other areas ? And also as i know the static objects are stored in the static area of the memory, but i found that in java they are stored in a section of the heap called PERMGEN and for the recent versions of java the storing is performed in a METASPACE section. Can anyone clarify for me pl

16th Aug 2018, 10:56 AM
De Vinci
17 odpowiedzi
+ 5
Java and C++ dosn't manage memory the same way, the main difference between is that Java use garbage collection and the JVM manages almost everything, you have no access to where the JVM puts your data and your code. whereas in C++ the program is directly executing on the machine and memory is managed through OS services. Everything works on the hardware, so you will have stack segments, data segments. In Java, everything is also represented at runtime, so you can ask an object what class it belongs to and what method is supports. In C++, classes have no representation in memory at runtime.
17th Aug 2018, 9:22 AM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 7
Dababi Mohamed Amine I didn't understand what you mean by "In C++, classes have no representation in memory at runtime", can you elaborate some more on this fact?
17th Aug 2018, 10:52 AM
Ipang
+ 4
This is a good article that shows you the differents parts of the memory. Note that Permgem is not part of the heap. https://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java
16th Aug 2018, 5:10 PM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 4
Dababi Mohamed Amine excellent link! thank you :)
17th Aug 2018, 10:44 AM
StealthyTaco
StealthyTaco - avatar
+ 4
Dababi Mohamed Amine Thank you for detailed answer, anyways, when you wrote "They are treated as any other variable and can be allocated on the stack ...", were you referring to classes or objects? Also can you please explain how a static method works in C++ given the fact that classes have no representations in memory during runtime.
17th Aug 2018, 11:43 AM
Ipang
+ 3
I read that Heap have 3 levels for objects that live long time. Know only about Permgen, Heam and Stack thats enough for me.
16th Aug 2018, 6:13 PM
Artem Muravlev
Artem Muravlev - avatar
+ 3
does not java have parts of memory like c and c++?
17th Aug 2018, 1:25 AM
De Vinci
+ 3
thank you khouya mohamed
17th Aug 2018, 10:42 AM
De Vinci
+ 3
@lpang In Java objects are created using the keyword new. They can only be instantiated in heap. Java objects can be accessed through reference variables, that hold the address of objects in heap. Objects with no references pointing to them are considered eligible for automatic garbage collection by the system In C++ classes are user defined types. They are treated as any other variable and can be allocated on the stack as regular local variables or on on the heap like in Java. Objects can be accessed through pointers. After an object has been created on the heap (with the new directive) it survives until someone destroys it explicitly using the delete directive. On the other hand, local objects, which are allocated on the stack, always have a defined life time such objects are automatically deleted when they go out of scope.
17th Aug 2018, 11:16 AM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 3
lpang i was referring to object. for static members they are stored with global variables. in fact, they live in the global memory which is separated from the heap and the stack. They are stored in the data segment (initialized data sections .data and uninitialized .bss) which is burned in the executable and have typically a fixed size(im not sure about the size).
17th Aug 2018, 12:57 PM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 3
Dababi Mohamed Amine Thank you so much for your answer, so my assumption, to conclude over this, in C++ classes don't have representation in memory at runtime. A class is stored in data segment, and when a static method of a certain class is needed to be invoked, the class is temporarily loaded into memory, the static method is invoked, and then the class representation is removed from memory? am I understanding this correctly?
18th Aug 2018, 9:40 AM
Ipang
+ 2
yes static members are stored in the data segment unlike the heap which is allocated by system in runtime. for the method of invocation im not sure that it works this way. to go in depth you can also read about virtual methods because they are stored differently in memory.
18th Aug 2018, 12:59 PM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 2
ok thanks..the data segment exists in the metaspace ?
18th Aug 2018, 7:14 PM
De Vinci
+ 2
thank you so much my friend..you were very helpful :)
18th Aug 2018, 9:38 PM
De Vinci
+ 1
please guys, static and global, are the same ?
18th Aug 2018, 6:01 PM
De Vinci
+ 1
not they are not the sa me. the main difference is the scope.   A global variable has global scope, and is visible everywhere in your program. A static variable can have local scope. In memory, the two of them resides in the data segment.
18th Aug 2018, 6:57 PM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 1
the data segment is part of your executable
18th Aug 2018, 7:21 PM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar