lesnar_56
lesnar_56

Reputation: 105

Memory Allocation in JVM

I am thinking about memory allocation in Java i.e in which memory location methods, fields and objects are stored.

Suppose I have 2 classes

import java.util.*;
class ABC
{
int field;
List<Integer>l;
}   
class XYZ 
{
int x,y,z;
static int p;
void fun(ABC a){
    a.field = 10;
    a.l = new ArrayList<Integer>(10);
    a.l.add(10);
}
}   
public class Simulator{
public static void main(String[] arg){
    XYZ tmp_1 = new XYZ();
    ABC tmp_2 = new ABC();
    tmp_1.fun(tmp_2);
    System.out.println(tmp_2.l);
}

}

Now where will the memory be allocated for each of the data members,functions and objects be allocated ?

My thoughts are objects, data-members will be stored in Heap but I am not sure about functions and their data members ?

Upvotes: 1

Views: 1895

Answers (3)

Stephen C
Stephen C

Reputation: 718678

Here's a breakdown of how the different things that you refer or allude to are stored:

  • Local variables and method / constructor parameters can contain either primitive values or references to objects or arrays. (They cannot contain the objects or arrays themselves.) Locals and parameters are stored in activation frames (to use the JLS terminology), and these frames are stored on a thread's stack. Thread stacks are non-heap memory in typical JVMs.

  • Objects and arrays are represented in heap memory.

  • Instance fields (containing primitive or reference values) are stored in objects, and hence in the heap.

  • Array elements (either primitive or reference values) and the length of an array are stored in the array and hence in the heap.

  • Static fields are stored in special frames called static frames. These frames are stored in the heap.

  • The code of Java methods (in byte-code and native code form) is typically represented by code blocks that are stored in the heap.


Note that the above is based on what happens in a typical JVM. In fact, the Java Language Specification does not mandate that things are stored in a stack or a heap. The terms stack memory and heap memory hardly appear in the JLS at all, and it is conceivable that other memory organization schemes could be used.

Also, "the heap" is an over-simplification, because a HotSpot JVM / GC typically divides the heap into areas with different characteristics. For instance the "permgen" area of the heap (where static frames and code blocks are allocated) is only garbage collected rarely.

Upvotes: 1

sinharaj
sinharaj

Reputation: 1093

Yes, all objects are allocated on the heap. I.e.: everything created with new will be placed on the heap. All fields of such objects are thus also on the heap.

I suppose you are referring to local variables and parameters in functions. These are placed on the stack (for each call of the function). Note, however, that when function variables/parameters are of reference types then the actual object they point to is on the heap but the variable/parameter itself will be on the stack (similar to a pointer in C/C++). On the other hand, function variables/parameters of primitive types (like int, double, etc.) will be entirely on the stack. A slightly related but relevant to the topic is the concept of boxing.

Functions themselves are static code and are stored in a special part of the memory where all executable code resides. Note that all instances of a class share the same functions (i.e. "new functions" are not created with newly created objects, even if these functions are instance functions). Thus, when you call an instance function on an object, a special this reference is passed to that function (it's an implicit argument to the function). This is how the function knows on which objects it should act.

Upvotes: 4

Amir Pashazadeh
Amir Pashazadeh

Reputation: 7282

To complement previous answer:

  1. The functions are stored in special area, but a reference to them is stored in the object (for non static functions).
  2. Reference to static functions is stored in another area (you can think they are stored just next to static fields) (static memory area).
  3. All objects are stored in heap (Object Pool), but the place of reference to them vary, local variables are in the stack, non static fields are stored in the object itself, so they are stored in the heap too.
  4. Static references are placed in another special part of the memory, (although if they are references to Objects, they point to heap).

Upvotes: 0

Related Questions