10

Okay, so I have been developing in Java for a little over a year now and am making an effort to dive deeper into the language and its best practices.

So here is what I know:

  • Java "passes by type" - that is primitives pass by copy and object reference pass by copy (references point to their object on the heap).

  • Primitive instance variables and references live in their class object in the heap and local primitives and references live on the stack (in their respective stack frame).

  • Perm Gen. memory space is where class meta data is stored (used for reflection).

  • The Heap has an Eden space where new objects are places, a Young space where objects who have survived a GC are kept and a Tenured space where long lived objects are placed.

So here is what I would like to understand:

  • Where do static and static final primitives and references live that the JVM is able to use a single instance?

  • Are static and static final objects stored in the Heap (I assume they are moved to tenured)?

  • What is considered the best practice in terms of the number of static final references in an application?

  • Will creating more static final references decrease the amount of Heap space in JVM?

I have read many different explanations about this (all differed) and would love if a seasoned veteran in the Java language could provide a good explanation. Thanks in advance!

jjNford
  • 4,940
  • 4
  • 35
  • 62
  • 1
    this SO question has some info on static classes http://stackoverflow.com/questions/3849634/static-allocation-in-java-heap-stack-and-permanent-generation – fasseg Mar 01 '12 at 13:42

1 Answers1

5

The young space includes the eden space and survivor spaces.

Where do static and static final primitives and references live that the JVM is able to use a single instance?

Its not defined, but in the Sun/Oracle JVM, static fields live in a special object for class fields. You have one instance per class loader so static fields can have multiple instances.

Are static and static final objects stored in the Heap (I assume they are moved to tenured)?

In the Sun/Oracle Java 7 they are. They could be in the Perm Gen or any where else.

What is considered the best practice in terms of the number of static final references in an application?

Keep them to a minimum.

Will creating more static final references decrease the amount of Heap space in JVM?

If you can change a final field into a static final field, this can save some space (if you have multiple instances) However, clarity is usually more important than performance. (And I would do it for clarity)

BTW: I have been developing in Java for 13 years.

So there can be multiple instances of static fields - does the JVM alter each instance

They are independent. Each classloader can load its own version of a class (the code doesn't have to be the same) and each gets its own static fields (they don't have to be the same either)

if a static field is changed (ie. static int instanceCount where instanceCount++ is executed on each object construction)?

No.

Also, Objects can be moved to Perm Gen?

No. Must some data which is not defined in one location can be anywhere depending on the implementation and version.

Is Perm Gen. considered to be part of the Heap?

It is part of the old gen = tenured + perm gen.

Young gen = eden + survivor space * 2

Maximum Heap Size limits the young gen & tenured total. Perm gen and direct memory have their own limits. Memory mapped files done follow any of these limits.

This is true for the default parallel collector and the concurrent mark sweep.

The G1 collector doesn't divide up the spaces the same way.


Links for more details

http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

Java heap terminology: young, old and permanent generations?

http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html

Community
  • 1
  • 1
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • thank you for your quick response and clarification on the Young Heap space. So there can be multiple instances of static fields - does the JVM alter each instance if a static field is changed (ie. static int instanceCount where instanceCount++ is executed on each object construction)? Also, Objects can be moved to Perm Gen? Is Perm Gen. considered to be part of the Heap? Sorry for so many questions, this is very helpful to me and I thank you much. If you don't mind just update your answer with these solution so others will find it easy to get to. Thanks again. – jjNford Mar 01 '12 at 14:05
  • I'd b interested in reading up more on this - could you provide some sources? (Or are things like that covered by the JVM-Spec?) – quaylar Mar 01 '12 at 14:26
  • @quaylar They are typically implementation details not covered by the spec. – Peter Lawrey Mar 01 '12 at 15:04
  • @quaylar, also check out http://redstack.wordpress.com/2011/01/06/visualising-garbage-collection-in-the-jvm/ – Zaki Mar 01 '12 at 17:48