3
public Class A {
    public static String s = "s";
    public static int i = 0;
    public int j = 1;
    public static String getStaticString() {
        int k = 2;
        return s;
    }
    public String getString() {
        int l = 3;
        return "something";
    }
}

In Java, static variables are stored in Perm generation of Heap Segment, and primitive local variables are stored in Stack Segment. So where is i, j, k, l stored? Where is function getString() stored?

3
  • "primitive variables are stored in Stack Segment" ... are you sure you read that right, or maybe there was a word missing, such as "primitive local variables"? I think what you said about static variables applies to all static variables of any type. Commented Oct 25, 2014 at 15:59
  • possible duplicate of where is a static method and a static variable stored in java. In heap or in stack memory Commented Oct 25, 2014 at 16:37
  • "Local variables" are stored in the stack segment. All other variables are stored somewhere in the heap. Static variables are store (conceptually, at least) with the Class object for the class, while instance variables are stored with each instance. Method bytecodes (both static and instance) are stored in C heap storage -- not part of the Java heap. Commented Oct 25, 2014 at 16:39

1 Answer 1

3

These are implementation details and we can't know for sure what each implementation does, without first reading and understanding its source code. As far as my knowledge and experience goes, the most reasonable things to assume (for a desktop JVM) are along these lines:

  • s and i are static variables. Static variables are probably allocated on the heap, in the permanent generation.
  • j is stored inside instances of class A. Class instances may live on the stack (if escape analysis for references can prove that the reference has automatic storage semantics - and they are small enough) or the heap (if escape analysis is not performed or is inconclusive or the instance is too large for the stack).
  • k is a local variable with automatic storage semantics, thus it should live on the stack. It's allocated when its containing method (getStaticString) is entered and it's deallocated when its containing method is exited.
  • l has the same semantics as k. The fact that its containing method (getString) isn't static, is irrelevant.
  • getString (and any other piece of user code, regardless of its linguistic attributes as static, non-static, etc) has two representations:
    • Its metadata and bytecode (AOT-compiled) are part of the class data of its containing class. Its lifetime in memory is likely to be linked with the loading/unloading of the class associated with this code, but not with any particular instance of that class. In other words, non-static methods aren't "created" every time you create an instance.
    • Its compiled code (JIT-compiled) should live permanently in a separate memory segment (a part of the JIT compiler's unmanaged heap, written to and then marked as executable), independent of the lifetime of the Java objects.
Sign up to request clarification or add additional context in comments.

4 Comments

getString has (in the general case) both a bytecode representation and a JITC representation. The first is stored ("conceptually") with the Class object for this class (though in practice probably in C heap, not "permgen") while the second is stored in JITC's executable heap segment (which is a sort of C heap).
@HotLicks Right! It didn't occur to me that the OP may be asking about the non-executable representation. Will edit.
for j, can we say only when create a new A instance like A a = new A(); then j is allocated? And a will be stored in heap segment, so j is stored in heap segment too?
@coderz When a is allocated, part of its allocated space is reserved for j. If a is on the heap, then j is also on the heap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.