Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Good practice to detect lapsed observers

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Good practice to detect lapsed observers

    Hi there.

    I am working a lot with the observer pattern in my current application. Each object is, on average, observing 3 other objects. And I fear that maybe sometimes I might miss to correctly remove observers that I have previously registered. This might lead to the lapsed observer problem and memory leaks which I would like to avoid.
    Unfortunately, there is no compile-time way of detecting this, so I thought about implementing a run-time detection mechanism to throw an exception when a lapsed observer is detected.

    But I dont quite now how to do this. I have one idea although it does not sound too solid to me:
    I will add a finalize() method to all my observable objects. In this method they will check if there are any observers left observing them. If there are, this will be logged to let me know.
    But I dont think this would be able to detect all of them. Are there any other thoughts or suggestions? Any input on this is greatly appreciated.

    Thanks in advance.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Good practice to detect lapsed observers

    How about using a WeakReference (I believe you had a post about this a while back?)

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Good practice to detect lapsed observers

    It solves the problem of leaking memory, but it is not as clean as simply removing the observers when they are not needed anymore. I would prefer it to have my objects clean up after them instead of having the observer lists filter themselfs for unused garbage. If that makes sense.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Good practice to detect lapsed observers

    It's a problem that has been solved before using WeakReferences. Agree that directly removing references when they no longer need to listen seems cleaner, but the more complex the architecture the higher the probability a few will slip through here and there. I'm not sure I would trust when finalize() is called (or whether it ever will, depending upon how the listener chain is connected). If you wish to enforce that each and every one is manually removed, you might consider just doing it brute force in debugging by throwing your program into a profiler like jvisualvm and run your software through some rounds of tests to see if/when objects accumulate after objects go out of scope followed by calls to System.gc

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Good practice to detect lapsed observers

    Do you have any personal experience with weak references? Because I dont and I dont know if the code I posted in the other thread is actually correct.
    The problem is that its very hard to test whether it works because of how unpredictable the garbage collector can be.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Good practice to detect lapsed observers

    Yes I have used WeakReferences, useful in this context as well as in caches. I didn't comment in the other thread you posted, but recall looking through it and had no problems. You could test it by constructing a simple example in which one or more objects will go out of scope, make a call to System.gc and then check the weakReference - although the GC made the 'best effort' after calling this method, every time I have tested things in this context they ran as expected (YMMV). In your program itself, I would recommend running it though a profiler in conditions which you know objects will go out of scope and should be garbage collected, execute a garbage collection (VisualVM has this feature) to see if the object count is what you expect. Doing so is very useful regardless, as one can discover unexpected issues worth rectifying along the way.

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Good practice to detect lapsed observers

    Thank you for the advice, I will see what I can do.
    by the way, I guess it doesnt hurt to have the finalize method log information as well, just for good measure.

Similar Threads

  1. Question about good coding practice
    By Cornix in forum Java Theory & Questions
    Replies: 7
    Last Post: August 29th, 2014, 08:32 AM
  2. Any good websites to practice programming?
    By SauronWatchesYou in forum The Cafe
    Replies: 1
    Last Post: April 17th, 2014, 08:06 AM
  3. Today is a very good day and I'm in a good mood
    By AHefphern in forum The Cafe
    Replies: 0
    Last Post: May 15th, 2013, 03:10 AM
  4. Java Practice !!
    By YogiB1810 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 28th, 2013, 09:00 AM
  5. Good examples for exam practice?
    By Twoacross in forum Java Theory & Questions
    Replies: 2
    Last Post: December 28th, 2011, 01:48 AM