6

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 ?

Community
  • 1
  • 1
Meher
  • 2,545
  • 3
  • 26
  • 53
  • Duplicate: [http://stackoverflow.com/q/405364/1073063](http://stackoverflow.com/q/405364/1073063) and [http://stackoverflow.com/q/6569557/1073063](http://stackoverflow.com/q/6569557/1073063). Moderator: Sorry, I linked to the wrong question when I flagged this. – Pablo Jun 07 '12 at 12:14

2 Answers2

5

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.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
3

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

Community
  • 1
  • 1
Houcem Berrayana
  • 3,052
  • 22
  • 40