1

Memory leaks in Java are not similar to C/C++ because of garbage collector of Java. Java's GC is able to remove all instances that are not referenced; however, if you forget to remove a reference to an instance it would cause memory leak. I got the meaning of this sentence but I do not know what it means by removing reference to an instance.

How to demonstrate keeping reference to an object and de-referencing it?

Take following code as an example :

 public String ObjPage(){
     ... 
     List<MyObject> myObj = retrieveObjs();
     return "SUCCESS";
 }
 public List<MyObject> retrieveObjs(){
       List<MyObject> objs = new ArrayList();
       ....
       return objs;
 }

Does this code de-reference its instances?

Jack
  • 6,430
  • 27
  • 80
  • 151
  • Yes when a method terminates the local variables are de referenced. Creating a memory leak is actually more difficult than you might think. Given there are a few areas that are prone to memory leaks, check out this post for more info http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java – ug_ May 14 '14 at 02:28

0 Answers0