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

java.lang.ref.SoftReference: OutOfMemoryError thrown before instances cleared

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: akC45999 Date: 10/01/98



      Description of the class java.lang.ref.SoftReference reads:

        All soft references to softly-reachable objects are guaranteed to have
        been cleared before the virtual machine throws an OutOfMemoryError.


      However, the attached test shows that a soft reference might not be cleared
      before OutOfMemoryError is thrown:

      ------------------------- file SoftReference0101.java
      import java.io.PrintStream;
      import java.lang.ref.*;


      public class SoftReference0101 {

        public static int run(String argv[], PrintStream out) {
      SoftReference ref=new SoftReference(new Object());

      Object[] memWaste=null;
      int arrSize, arrSizeNext=100000;

      eatMem:
      for (;;) {
      arrSize=arrSizeNext;
      try {
      Object [] newMemWaste=new Object[arrSize];
      out.println("allocation of "+arrSize+": succeeded.");
      newMemWaste[0]=memWaste;
      memWaste=newMemWaste;
      if (ref.get()==null) break eatMem; // test passed
      arrSizeNext=arrSize*2;
      } catch (OutOfMemoryError e) {
      out.println("allocation of "+arrSize+": failed.");
      if (ref.get()==null) break eatMem; // test passed
      out.println("SoftReference.get()!=null");
      return 2;
      }
      }
      out.println("passed");
          return 0;
        }

        public static void main(String args[]) {
      run(args, System.out);
        }

      }

      ---------------------------------

      Running the test:

      novo64% java -fullversion
      java full version "JDK-1.2fcs-L"
      novo64% javac SoftReference0101.java
      novo64% java SoftReference0101
      allocation of 100000: succeeded.
      allocation of 200000: succeeded.
      allocation of 400000: succeeded.
      allocation of 800000: succeeded.
      allocation of 1600000: succeeded.
      allocation of 3200000: succeeded.
      allocation of 6400000: succeeded.
      allocation of 12800000: failed.
      SoftReference.get()!=null
      novo64%

      ======================================================================

            mr Mark Reinhold
            rfqsunw Rfq Rfq (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: