Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the actual memory place for static variables?

A static variable is allocated for the entire duration of a program's execution, so neither stack nor heap are convenient for it. Then where is that variable? Shouldn't there be some place where it is loaded from?

like image 591
Reuben Avatar asked Jul 04 '11 09:07

Reuben


People also ask

Where the memory is allocated for static variables?

2) Static variables are allocated memory in data segment, not stack segment.

Which memory is used by static variables?

After the java 8 version, static variables are stored in the heap memory.

Are static variables stored in RAM?

Static variables are stored in RAM, just like your global variables.

Where are memory variables stored?

Variables are usually stored in RAM. This is either on the heap (e.g. all global variables will usually go there) or on the stack (all variables declared within a method/function usually go there). Stack and Heap are both RAM, just different locations.


1 Answers

We have 3 segments in our memory:

  1. Stack Segment — contains local variables and Reference variables (variables that hold the address of an object in the heap).

  2. Heap Segment — contains all created objects in runtime, objects only plus their object attributes (instance variables).

  3. Code Segment — the segment where the actual compiled Java bytecodes resides when loaded. Static members (variables or methods) are called class members, meaning they reside where the class (bytecode) resides, which is in the Code Segment.

like image 111
Sourav Avatar answered Sep 28 '22 00:09

Sourav