13

This is a follow-up question to How are static arrays stored in Java memory? .

So global variables in C/C++ are stored in the static data segment of memory. But what about static class variables in Java/C++?

It can't be the static data segment because you don't know what/how many classes are going to be referenced throughout the duration of your program (because of reflection). It's definitely not the stack because that makes no sense. Storing it on the heap is also kind of iffy.

Community
  • 1
  • 1
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • 2
    Is this a C++ or a Java question? In C++ static members are just global variables. – Kerrek SB Jul 11 '11 at 19:26
  • @Kerrek: I believe he is wondering about Java memory management of `static` that mimics C/C++ management of *similar* concepts. – user7116 Jul 11 '11 at 19:28
  • @Six: Sure. I just found "what about static class variables in Java/C++" a bit confusing. I look forward to the Java-related answers. By the way, does Java *have* an explicit "heap"? – Kerrek SB Jul 11 '11 at 19:28
  • I guess I'm asking about both C++ and Java. C doesn't have objects so how memory is laid out is pretty simple. For objects, instance variables are stored on the heap. However what I don't understand is where class variables are stored. – tskuzzy Jul 11 '11 at 19:30
  • How are instance variables stored on the heap? In C++ you can perfectly well put your instance variables on the stack by declaring them with automatic storage. I think you do really need to separate this into two questions, one for Java and one for C++... – Kerrek SB Jul 11 '11 at 19:37
  • 2
    In C++ the situation is simple because there is no (portable) way to load a class at runtime. In C++ class static data members are just global variables with a fancy name, they are created when program starts and destroyed when programs end... exactly like globals. – 6502 Jul 11 '11 at 19:39
  • @Kerrek SB: Really? This question seems to agree that member variables are always stored on the heap: http://stackoverflow.com/questions/2820477/class-members-allocation-on-heap-stack-c – tskuzzy Jul 11 '11 at 19:41
  • @Tskuzzy: The question doesn't say "always" at all -- it says that that particular class whose constructor allocates heap memory causes objects to ... allocate heap memory. But you can write lots of other classes that do not acquire heap memory by themselves. – Kerrek SB Jul 11 '11 at 19:55

3 Answers3

5

In Java, at a low level, class static variables are indeed stored on the heap, along with all other class metadata. To Java, they look like globals, but to the JVM's low level heap management routines, they're dynamic data (although they may be treated slightly specially in order to improve GC efficiency, since they're likely to be long lived). After all, classes can be unloaded by unreferencing their classloader.

As for whether it's the same as the C malloc(), not likely. Most JVMs take control of their heaps at a low level; they grab a chunk of memory from the OS and divvy it up themselves. As such, most Java data, including static data, are not stored in the malloc heap, but rather in a separate heap managed by the JVM.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
3

Java has a "permanent" heap where it puts class metadata. So the "roots" of the static values are in the permanent heap. The values are reference values (class objects) the values themselves are in the regular heap.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
1

Static variables will not be stored in Heap.. They are part of Data Segment. Local variables will be stored in - Stack; Instance variables will be stored in - Heap; Class variables(Static) will be stored in - Data Segment. These variables will be shared across all objects of that class.. Your final machine equivalent java code will be stored in - Code/text segment.