Results 1 to 9 of 9
  1. #1
    Navatha is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default memory leaks in Java

    Hi All ,
    Can you tell me .when we will have memory leaks in java.And how to avoid the memory leaks in java.

    Thanks in Advance
    Navatha

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    One sort of memory leak is an object that isn't being used but still has a reference to it being held somewhere. For example unused listeners can often be left attached when they should have been removed.
    Avoid by good programming practices. If you open a file, close it. If you add a listener, remove it.

  3. #3
    Navatha is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Is this the only type of memory leak or there are any other type of memory leaks.One method is using garbage collection .The other one is as you said Norm. What type of memory leaks we encounter with collections.
    Thanks in advance
    Navatha

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Garbage collection is not a method of creating memory leaks. Garbage collection removes unused blocks of memory so that memory does NOT leak.

    How does garbage collection work?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Garbage collection is not a method of creating memory leaks.
    Have a look at this one:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Leak {
    	private static List<Leak> zombies= new ArrayList<Leak>();
    	
    	protected void finalize() {
    		zombies.add(this);
    	}
    }
    kind regards,

    Jos

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    The solution to that is to never write stuff in a finalize()...:)
    That really is a red flag warning during a code review.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Agreed with Tolls. That's not the garbage collector's fault, that's a bad coder's fault. ;)

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    You'd be surprised, however, how often people use finalize, though. And i almost always leads to these types of problems.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Agreed with Tolls. That's not the garbage collector's fault, that's a bad coder's fault. ;)
    It's called 'resurrection' and the JVM's weak defense against it is to call those finalizer methods only once by the gc; but I agree: finalizers are not of much use (except for resource deallocation (if it isn't too late yet)).

    kind regards,

    Jos

Similar Threads

  1. Java Memory Issue
    By personal in forum Advanced Java
    Replies: 12
    Last Post: 01-07-2012, 02:05 PM
  2. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  3. Axis 1.4 xerces memory leaks
    By hlaprade in forum Advanced Java
    Replies: 0
    Last Post: 12-11-2007, 12:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •