Alex Rivera | Logout

About Memory Management in Java and C++

Asked 2009-01-14T00:05:06.953
10

Well, I've been given an assignment to basically figure out how memory allocation works for whatever language I'll be using. After some research, I have some questions and doubts which I'd like to get some insight in. For example:

I read here that Java specifies exactly how the stack contents are organized. Looking at the JVM spec structure, it basically says the stack contains frames, and that the frames contain whatever is inside the class by properly allocating the variables and functions. Maybe I am missing something here, but I don't understand how this is any different than what C++ does. I ask because the first link says Java's specification of stack contents avoid compiler incompatibilities.

Also, I have yet to find how the memory segments are exactly organized on top of each other. For example, I know the memory is divided into global variables, call stack, heap and code for C++, yet I don't know if the heap's address is higher than the stack's, or if that depends on the implementation. I also wonder whether a Java program has more than that, and how it would be laid out as well. I imagine there is a standard, since the JVM has to know where it all is to use it, though I suppose it could just have the pointers and leave the rest to the OS. I imagine too, that there must be at least a de facto standard.

Another the thing I don't understand is the runtime constant pool. It's supposed to be "a per-class or per-interface runtime representation of the constant_pool table in a class file", but I don't think I understand what it does. It seems to have a tag to indicate what type the structure in question is? Then the name of the structure (given by the programmer or assigned by the underlying system?) Then it seems the rest of it varies w

Edit
Report

1 Answer

4

What exactly was the task that you were given?

The main difference between Java and C++ is that Java is garbage collected by the VM, whereas in C++ the program is directly executing on the machine and memory is managed through OS services.

Regarding the stack, a frame is just a more "official" and standard form of what C++ compilers do. C++ compilers just put things on top of each other in the stack as you move from call to call. In Java the term is frame, and because compiled Java code is supposed to run on any platform, there are very clear standards on how that takes place. In C++, each compiler can treat the stack differently (e.g., even by the nature of the word size).

In Java, everything runs within the VM which manages everything, though it delegates some stuff to the environment. In other words, you have no access to where the JVM puts your data and your code, and your code may never even become a real "code segment". In other words, this can't really be answered. In C++, everything works on the hardware, so you will have stack segments, data segments, etc. Look in information about C++.

In C++, classes have no representation in memory at runtime; in fact, you can compile C++ into C and then compile the results into assembly. In Java, everything is also represented at runtime, so you can ask an object what class it belongs to and what method is supports. Hence, each class file has a "constant pool" where the strings representing those things like method names, field names, etc. appear. The actual class definition refers to the pool. So in other words, that has very little to do with stack frames. Stack frames are where method parameters, local variables, and return values are stored.

answered 2009-01-14T00:17:28.303

Your Answer