title: clearing or set null to objects in java
body: <p>I was recently looking into freeing up memory occupied by Java objects. While doing that I got confused about how objects are copied (shallow/deep) in Java and how to avoid accidently clearing/nullifying objects while they are still in use.</p>
  
  <p>Consider following scenarios:</p>
  
  <ol>
  <li>passing a <code>ArrayList&lt;Object&gt;</code> as an argument to a method.</li>
  <li>passing a <code>ArrayList&lt;Object&gt;</code> to a runnable class to be processed by a thread.</li>
  <li>putting a <code>ArrayList&lt;Object&gt;</code> into a <code>HashMap</code>.</li>
  </ol>
  
  <p>Now in these case, if I call <code>list = null;</code> or <code>list.clear();</code>, what happens to the objects? In which case the objects are lost and in which cases only the reference is set to null?</p>
  
  <p>I guess it has to do with shallow and deep copying of objects, but in which cases does shallow copying happens and in which case does deep copy happens in Java?</p>