0

This question is regarding memory management & GC in java.

Since java does not have true static classes like C#, what is the scope of static members in a class such as below:

public class Test {
public static String myVariable;
}

Lets say Test.myVariable is set / called in another class Foo.

Is Test.myVariable kept alive for as long as Foo is alive?

OR

Will Test.myVariable be kept alive for as long as the application domain is ?

When would myVariable go out of scope ?

AlexVPerl
  • 7,652
  • 8
  • 51
  • 83
  • 1
    not at all. public static members are accessible by any class that has access to `Test`. –  Jun 24 '15 at 21:33
  • I'm considering scenario where Test.myVariable was accessed once. – AlexVPerl Jun 24 '15 at 21:34
  • All, I've clarified the question as it deals with memory scope not access scope. – AlexVPerl Jun 24 '15 at 21:35
  • What did you find in the documentation that you did not understand? It sounds as though it should be in scope for as long as `Test` is loaded. – PJTraill Jun 24 '15 at 21:37
  • @PJTraill what do u mean by loaded? Test would never be instantiated it self. Only Test.myVariable is called - at which point does it go out of scope ? – AlexVPerl Jun 24 '15 at 21:39
  • What I meant is that – as Paul remarked – it is accessible to any class that has access to _Test_, but that if _Test_ is loaded by some _ClassLoader_ and accessed via Reflection (rather than accessed by other code in the same code base), it need not stay loaded for ever. – PJTraill Jun 24 '15 at 21:46
  • Thanks @PJTraill Please review the question as it deals with memory management not accessibility modifier keywords (public, protected, etc). – AlexVPerl Jun 24 '15 at 21:48
  • Are you using “scope” in the sense of the Specification: (Scope of a declaration)[http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.3]? Scope of a declaration is “is the region of the program within which the entity ... can be referred to using a simple name, provided it is visible”, i.e. it is a static rather than a dynamic property, and has nothing to do with memory management. Perhaps you need to change your terminology. – PJTraill Jun 24 '15 at 21:55
  • Question is about variable memory scope. I'm not sure why the verbiage is so confusing to many readers. Saying "variable is out of scope" always meant memory scope in other words Garbage Collected or eligible for - at least for me :) – AlexVPerl Jun 24 '15 at 21:57

4 Answers4

2

I guess you mean when it will be available for the garbage collection.static members are candidate for garbage collection when the class loader which was responsible for loading the class is itself a candidate for garbage collection. For example suppose class Test was loaded by a class loader object say loader1.So when loader1 is eligible for GC then class Test and its static variable ,in this case myVariable are also eligible for GC Check out here

Sarfaraz Khan
  • 2,166
  • 2
  • 14
  • 29
  • Thanks. So in this case practically speaking, do you mean it would be eligible for GC when Foo is eligible ? – AlexVPerl Jun 24 '15 at 21:51
  • No when the class Loader which has loaded Test class will be eligible for garbage collection – Sarfaraz Khan Jun 24 '15 at 21:54
  • @AlexVPerl check my answer I have added an explanation with respect to your Test class and myVariable. – Sarfaraz Khan Jun 24 '15 at 22:07
  • Thanks @Sarfaraz this makes sense. – AlexVPerl Jun 25 '15 at 18:54
  • The link https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html%23jls-12.7 seems broken; should presumably be to _Java Language 12.7: Unloading of Classes and Interfaces_ at http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.7 ? – PJTraill Jun 25 '15 at 19:58
0

public access means it is available for all. There is no distinction of public between static and instance variables.

When would myVariable go out of scope ?

Not sure what you mean by variable going out of scope. Scope is public so it will be available everywhere. JVM lifetime in terms of garbage collection, depends on the reference to the class.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

your class is public. your static member is public. It will not go out of scope.

Raman Shrivastava
  • 2,923
  • 15
  • 26
0

I understand your concern is to when it is garbage collected. Before proceeding ahead you need to understand memory management

Note that only the variables and their technical values (primitives or references) are stored in PermGen space.

If your static variable is a reference to an object that object itself is stored in the normal sections of the heap (young/old generation or survivor space). Those objects (unless they are interal objects like classes etc.) are not stored in PermGen space.

Example:

static int i = 1; //the value 1 is stored in the permgen section 
static Object o = new SomeObject(); //the reference(pointer/memory address) is stored in the permgen section, the object itself is not.
Mudassar
  • 3,135
  • 17
  • 22
  • Thanks but you're explaining on which sections of memory variables are stored. I come from C# background so I'm familiar with heap vs stack differences, however not so much with PermGen - does that imply that object will live longer? – AlexVPerl Jun 24 '15 at 22:00
  • I strongly recommend you to go through these links that explain memory management. https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html. http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning – Mudassar Jun 24 '15 at 22:03
  • Kindly let me know if you still have issues after going through the doc – Mudassar Jun 24 '15 at 22:25
  • Kindly let me know if your concern is resolved else kindly accept the answer and close the question – Mudassar Jun 25 '15 at 09:46
  • Sry @Mudassar but your reply, even though informative, but does not answer the actual question posted. – AlexVPerl Jun 25 '15 at 18:55
  • No worries buddy, just saw Sarafaraz's answer, it's more specific – Mudassar Jun 25 '15 at 19:41
  • What does your expression _PermGen_ mean and where does it come from? I cannot find “perm” in the page you link to, nor in its associated index. – PJTraill Jun 25 '15 at 20:08
  • @PJTraill have a closer look at the links mentioned, you will find it – Mudassar Jun 25 '15 at 21:37
  • Sorry, it is not there, but it is in [Java SE6 HotSpot VM GC Tuning](http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html). – PJTraill Jun 26 '15 at 10:09
  • Seems you missed out reading the second link http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning which shows the understanding of permgen – Mudassar Jun 26 '15 at 10:12
  • Yes, sorry, I see now, I mistook it for one link abbreviated with “...” — but the Oracle reference I gave seems more informative. – PJTraill Jun 26 '15 at 10:13