What with static memory in java?

This question relates in particular to the java language. I understand that there is a static memory protector for static code.

My question is: how does this static memory fill up? Is a static object placed in static memory on import or on the first link? Also, are the same garbage collection rules applied to static objects as all other objects?

public class Example{ public static SomeObject someO = new SomeObject(); } /********************************/ // Is the static object put into static memory at this point? import somepackage.Example; public class MainApp{ public static void main( Sting args[] ){ // Or is the static object put into memory at first reference? Example.someO.someMethod(); // Do the same garbage collection rules apply to a // static object as they do all others? Example.someO = null; System.gc(); } } 
+19
java memory-management static
Jan 01 '09 at 19:40
source share
6 answers

Import does not correlate with any instructions in compiled code. They set aliases for use only at compile time.

There are some reflective methods that allow you to load a class but not yet initialized, but in most cases you can assume that whenever a class is referenced, it has been initialized.

Static member initializers and static blocks are executed as if they were a single static initializer block in source code order.

The object referenced by the static member variable is strictly referenced until the class is unloaded. A regular ClassLoader never unloads a class, but those used by application servers run under the right conditions. However, this is a complex area and is the source of many hard-to-diagnose memory leaks, but another reason not to use global variables.




As a (tangential) bonus, here's a tricky question:

 public class Foo { private static Foo instance = new Foo(); private static final int DELTA = 6; private static int BASE = 7; private int x; private Foo() { x = BASE + DELTA; } public static void main(String... argv) { System.out.println(Foo.instance.x); } } 

What will this code print? Try it and you will see that it prints "6". There are several things here, and one of them is the order of static initialization. The code runs as if it were written as follows:

 public class Foo { private static Foo instance; private static final int DELTA = 6; private static int BASE; static { instance = null; BASE = 0; instance = new Foo(); /* BASE is 0 when instance.x is computed. */ BASE = 7; } private int x; private Foo() { x = BASE + 6; /* "6" is inlined, because it a constant. */ } } 
+34
Jan 01 '09 at 20:04
source share

Usually there is no such thing as β€œstatic” memory. Most vm have a constant generation of heaps (where classes are loaded) that usually do not collect garbage.

Static objects are allocated just like any other object. But, if they live long, they will be moved between different generations in the garbage collector. But they will not fall into the permgenspace area.

If your class constantly clings to this object, it will be released only after vm exits.

+6
Jan 01 '09 at 19:50
source share

This static variable some0 is initialized as soon as your class references your code. In your example, this will be done on the first line of your main method.

You can verify this by creating a static initializer block. Place a breakpoint in this initializer block and you will see when it will be called. Or even simpler ... set a breakpoint in the SomeObject constructor.

+3
Jan 01 '09 at 19:50
source share

The initialization of static variables is described in section 2.11 Static Sun Initializers JVM spec. The specification does not define the implementation of the Garbage collection, however, I assume that the rules for garbage collection for static objects will vary depending on your virtual machine.

+3
Jan 01 '09 at 20:01
source share

It should be noted that only a pointer (or any other primitive type) is stored in PermGenSpace (this is the proper name for the area where static material is stored).

Thus, the object referenced by the pointer is on the regular heap, like any other object.

+2
Jan 02 '09 at 0:16
source share

If the static field is modified to refer to another object, the source object that the static field points to is eligible for the GC, like any other object.

It can also be free (even if not null) if the class itself is unloaded and the entire graph of the object is cut from the heap. Of course, when a class can be unloaded, this is a good topic for many other questions ... :)

0
Jan 02 '09 at 4:13
source share



All Articles