8

I am creating a sample demo program for make me understand that how can I deallocate the reference of static variables, methods in java using garbage collector?

I am using Weak Reference for not preventing the garbage collector.

Class Sample

public class Sample {

    private static String userName;
    private static String password;
    static {
        userName = "GAURAV";
        password = "password";
    }
    public static String getUserName(){
        return userName;
    }
    public static String getPassword(){
        return password;
    }
}

Class User

import java.lang.ref.WeakReference;

public class User {

    public static void main(String[] args) {
        /**
         * Created one object of Sample class
         */
        Sample obj1 = new Sample();
        /**
         * I can also access the value of userName through it's class name 
         */
        System.out.println(obj1.getUserName()); //GAURAV
        WeakReference<Sample> wr = new WeakReference<Sample>(obj1);
        System.out.println(wr.get());  //com.test.ws.Sample@f62373
        obj1 = null;
        System.gc();
        System.out.println(wr.get()); // null
        /**
         * I have deallocate the Sample object . No more object referencing the Sample oblect class but I am getting the value of static variable. 
         */
        System.out.println(Sample.getUserName()); // GAURAV
    }

}
Rakib Ansary
  • 4,078
  • 2
  • 18
  • 29
gaurav kumar
  • 171
  • 2
  • 6
  • 15

4 Answers4

21

static fields are associated with the class, not an individual instance.

static fields are cleaned up when the ClassLoader which hold the class unloaded. In many simple programs, that is never.

If you want the fields to be associated with an instances and cleaned up then the instance is cleaned up, make them instance fields, not static ones.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Also, aren't string literals never GC'd anyway, because they're kept in the String literal pool? Or is that also on a per-classloader basis? – Louis Wasserman Oct 29 '12 at 17:51
  • 1
    AFAIK String literals are cleaned up. If you intern() lots of random strings and discard them you won't get an OOME. Where interned strings are a problem is that up to Java 6 they were in the PermGen and it was possible to get an OOME filling this space when you had plenty of heap. In Java 7 String literals are in the heap. – Peter Lawrey Oct 29 '12 at 18:05
  • Hmmm, I am running a test with Java 6 creating lots of String.intern() objects and it is running with a max perm size of 64M. However the perm gen size is growing to 6 GB which isn't what I expected. – Peter Lawrey Oct 29 '12 at 18:19
  • What about 'private static' fields? And what about Android's advice on using statics: 1) http://developer.android.com/guide/faq/framework.html 2) http://developer.android.com/training/articles/perf-tips.html#PreferStatic – IgorGanapolsky Jul 10 '14 at 16:44
5

Other than the program, to answer your question

  1. No. Methods are not garbage collected because they don't exist in the heap in the first place.

  2. Static variables belong to the Class instance and will not be garbage collected once loaded (for most of the general Classloaders)

Arun Manivannan
  • 4,213
  • 3
  • 29
  • 38
4

System.gc() does not force garbage collector to run. It is just a suggestion to JVM that probably it is a good time to run garbage collector. See this question - When does System.gc() do anything

Community
  • 1
  • 1
user1700184
  • 1,611
  • 2
  • 16
  • 23
3

You should understand that System.gc(); does not call garbage collector. It just politely asks GC to remove some garbage. GC decides what to do and when to start itself. So, do not expect that you will see any immediate effect when calling System.gc();, assigning null to variable etc.

GC removes all objects that cannot be accessed by any way. So if code exited block where variable was defined the object can be removed. Assiging null removes the reference. Weak reference does not prevent GC from removing the object.

I hope this explanation helps.

AlexR
  • 114,158
  • 16
  • 130
  • 208