Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where the memory allocated when we declare static? [duplicate]

Tags:

java

android

Possible Duplicate:
static allocation in java - heap, stack and permanent generation

its a small confusion...defining static to class, methods and variables.In this three cases where thus the memory allocated. ? My boss is familiar with C,he says only variables are in heap memory and rest (static classes and static methods) will remain in main memory. is that ture? any explanation.?

one more in android using static class and static methods is a best practice ?

like image 521
Meher Avatar asked Jun 07 '12 12:06

Meher


People also ask

Where the memory is allocated for static variables?

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

How is memory allocated for static members?

Note: Memory for member functions and static data members is allocated per class and not per object. The class sample has no data member(except static count), but this does not mean no memory space will be allocated to the objects of Sample. In such cases, minimum memory is set aside for object.

Does static allocate memory?

Static Memory Allocation: Static Memory is allocated for declared variables by the compiler. The address can be found using the address of operator and can be assigned to a pointer. The memory is allocated during compile time.

Who will allocate the memory for static variables in Java?

Memory allocation for a static variable happens only once in the class area when the class is loaded in the memory. It is also known as a class variable. It is common to all the objects of the class. In this, a single copy of a static variable is created and shared among all the objects of the class.


2 Answers

Try this,

static members are stored in Method Area.

Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share the same data.

Non-heap Memory

It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘Method Area’.

Method Area

As given in the last line, method area is part of non-heap memory( A special heap area). It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.

The above three (heap memory, non-heap memory and method area) are the main jargon when it comes to memory and JVM.

Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share the same data.

like image 145
Kumar Vivek Mitra Avatar answered Oct 02 '22 05:10

Kumar Vivek Mitra


Static variables are saved in the same place as the Classes declaration (methods and attributes, etc). 1). Classes (loaded by the classloaders) go in a special area on heap called Permanent Generation, and static field too go to the same place as they are common to each instance of the class. For more details :

see this answer

like image 40
Houcem Berrayana Avatar answered Oct 02 '22 05:10

Houcem Berrayana