title: Why is this being garbage collected
body: <p>My developers and I are having an issue with objects being garbage collected in our application when we don't want them to be.  We are using Java with Weblogic 10g3.  We are programming a singleton pattern to handle all of our JMS connections.</p>
  
  <p>There are two classes involved:</p>
  
  <pre><code>public class JMSObject {
  ...
  private MessageProducer _producer;
  private MessageConsumer _consumer;
  ...
  // standard get/set procs... etc.
  }
  
  public class JMSFactory {
  ...
  // Hashmap sessions with key == ConnectionFactory Name
      Hashmap&lt;String, List&lt;Session&gt;&gt; _sessions;
  
  // Hashmap of JMSObjects with key == ConnectionFactory Name + JMS Queue Name
      Hashmap&lt;String, List&lt;JMSObject&gt;&gt; _jmsobjects;
  ...
  // standard get/set &amp; necessary sington functions
  }
  </code></pre>
  
  <p>The init method of the Servlets calls the JMSFactory singlton method, any new Sessions are placed in the _sessions Hashmap and new MessageConsumer/MessageProducers are created as a JMSObject and placed in the _jmsobjects Hashmap, in the appropriate List.</p>
  
  <p>The problem is that when the system is running the JMSObjects in the list get garbage collected after some time (sometimes in 5 minutes other times after a few hours.)  We looked at this for a few days but could not find any reason for the JMSObjects to be garbarge collected.  Since the JMSFactory has a reference to them why would the gc destroy them?</p>
  
  <p>In the end we fixed it by changing the classes as follows(without changing method interfaces):</p>
  
  <pre><code>public class JMSObject {
  ...
  private List&lt;MessageProducer&gt; _producers;
  private List&lt;MessageConsumer&gt; _consumers;
  ...
  // standard get/set procs... etc.
  }
  
  public class JMSFactory {
  ...
  // Hashmap sessions with key == ConnectionFactory Name
      Hashmap&lt;String, List&lt;Session&gt;&gt; _sessions;
  
  // Hashmap of JMSObjects with key == ConnectionFactory Name + JMS Queue Name
      private Hashmap&lt;String JMSObject&gt; _jmsobjects;
  ...
  // standard get/set &amp; necessary sington functions
  }
  </code></pre>
  
  <p>So far in testing the JMSObjects are not being gc'ed. It has been running for 2 days.</p>
  
  <p>Can someone explain why the indirect reference is causing the JMSObject to get gc'ed?  And why the Sessions in the _sessions Hashmap was not getting gc'ed?  Does it have anything to do with the fact the Sessions are built in Javax types and the JMSObject is something we wrote?</p>