Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4065027

Change in class loading/unloading behavior between 1.0.2 and 1.1.3

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.1.3
    • hotspot
    • None
    • sparc
    • solaris_2.5.1

      "Charlie McDowell" <###@###.###> writes:
       
      I believe that the way you do class unloading is incorrect. The
      attached example demonstrates how a program gets different behavior
      when GC is called which causes a class with a static field to be
      unloaded. Try it with and then without the System.gc line.

      class Example
      {
          public static void main(String[] args)
          throws java.io.IOException, ClassNotFoundException,
          IllegalAccessException, InstantiationException
          {
              int count = 0;
              SomeInterface class_one_object;

              /* load the class ClassOne and instantiate an instance of it */
              /* ClassOne uses ClassTwo which will thus get loaded also */
              Class theClass = Class.forName("ClassOne");
              class_one_object = (SomeInterface)theClass.newInstance();

              /* remove all references to the class and the instance */
              class_one_object = null;
              theClass = null;

      System.gc(); /* force garbage collection which also unloads classes */
              
              /*loads and instantiates ClassOne again. ClassTwo also gets reloaded*/
              class_one_object = (SomeInterface)new ClassOne();
          }
      }
      // File ClassOne.java:
      // public class ClassOne implements SomeInterface{
      // public ClassOne(){
      // new ClassTwo();
      // }
      // }

      // File SomeInterface.java:
      // public interface SomeInterface {
      // }

      // File ClassTwo.java:
      // public class ClassTwo {
      // static int counter=0;
      // public ClassTwo(){
      // counter++;
      // System.out.println("ClassTwo has counter = " + counter);
      // }
      // }

            sliangsunw Sheng Liang (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: