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

(ref) A stronger reference then SoftReference

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      In the javadocs of the class java.lang.ref.SoftReference it is stated that "Soft references are most often used to implement memory-sensitive caches." A memory sensitive cache should free objects when a critical memory size is reached and not before. However, a SoftReference only guarantees that an object will be cleared before the virtual machine throws an OutOfMemoryError and in practice, this means that the object may be cleared *at any time*. I propose a new Reference called a LightReference (or some better name) which is a stronger reference then SoftReference. A LightReference should guarantee that an Object is cleared if an OutOfMemoryError *would have* been thrown. In this way, Objects that are Lightly Referenced will stay in memory until it is absolutely necessary for them to be removed and therefor increase the number of cache hits because the number of objects removed will be minimized.

      JUSTIFICATION :
      This enhancement is necessary because it will ease development of memory-sensitive caches, perhaps even make it possible. Also, it is currently impossible to implement this feature in pure Java because Java's APIs regarding memory usage and allocation are too limited. Thus, this feature must be implemented closer to the GC in the stack.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      //start application.
      SoftReference ref = new SoftReference(new BigObject());
      //still only using a "normal amount of memory"
      //because we have only created one BigObject so far
      assert(ref.get() != null) //should not fail unless we *would* have run out of memory
      ACTUAL -

      //start application.
      SoftReference ref = new SoftReference(new BigObject());
      //still only using a "normal amount of memory"
      //because we have only created one BigObject so far
      if (ref.get() == null) //<-- this may be the case and we
      //dont want it to be.

      ---------- BEGIN SOURCE ----------
      public class TestClass {
          
          public static void main(String[] args){
              SoftReference ref = new SoftReference(new int[900000]);
              while(true){
                  if(ref.get() == null){
                      break;
                  }
                  int[] i = new int[900000];
                  //int[] i2 = new int[900000];
                  //uncommenting the above line does not create an out of
                  //memory exception, therefor ref did not *have* to be cleared
                  try{
                      Thread.sleep(100);//this just makes this process easier to kill
                  }catch(InterruptedException ie){
                      ie.printStackTrace();
                  }
              }
              System.out.println("This does not have to happen");
          }
          
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            mr Mark Reinhold
            Reporter:
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: