Results 1 to 10 of 10

Thread: [RESOLVED] Forcing a class to "die" ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    [RESOLVED] Forcing a class to "die" ?

    Here's the problem - I have a plugin loading mechanism that dynamically loads classes like this:

    - Define a variable as the class
    - put the variable in an arraylist

    The problem now is that when I attempt to unload such a plugin using .remove() with the arraylist, the class instance is still there. I noticed this when one of the plugins (a basic HTTPd) was still listening on a port when it should have been unloaded.

    I've already tried using
    System.runFinalization();
    System.gc();

    Thanks for any help!
    Last edited by MalcolmCarmen; Jan 16th, 2006 at 03:00 PM.
    ^_^

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Forcing a class to "die" ?

    This is one of two problems:-

    1- There still is a dependancy using the object
    2- The finalize() method in the class is manipulated so you can never erase the object while your program is still running
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Forcing a class to "die" ?

    Quote Originally Posted by MalcolmCarmen
    I've already tried using
    System.runFinalization();
    System.gc();

    Rumors have it that you should call System.gc() twice. Have a go at it and see if it really works.

    It is said that the first call marks the class loader and the second cleans it up. Not sure if it's true or if it will even work, but it's worth a try.


    Oh, and I don't think you can unload something that's in use See if you can stop it somehow first.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Forcing a class to "die" ?

    Quote Originally Posted by System_Error
    Rumors have it that you should call System.gc() twice. Have a go at it and see if it really works.

    It is said that the first call marks the class loader and the second cleans it up. Not sure if it's true or if it will even work, but it's worth a try.


    Oh, and I don't think you can unload something that's in use See if you can stop it somehow first.
    As far as I know, This problem was fixed in 1.4
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Forcing a class to "die" ?

    I'm not sure of any details about it, or even if it's true. Would be nice to know if it's true, but if the classes are still running then it won't matter. He somehow needs to stop them from running.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: Forcing a class to "die" ?

    It appears my solution is to just load these classes as threads - not only does it speed up execution of my app, but it allows me to at least kill the instances easier. This app is large enough that tracking down object references left over would be a very unfun task!
    ^_^

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Forcing a class to "die" ?

    Quote Originally Posted by MalcolmCarmen
    It appears my solution is to just load these classes as threads - not only does it speed up execution of my app, but it allows me to at least kill the instances easier. This app is large enough that tracking down object references left over would be a very unfun task!
    You didn't say your classes are already compiled and runnable.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Forcing a class to "die" ?

    Killing threads is a bad idea.

    Just create an interface that all these classes have to implement. Give it a method shutdown() that requires the plug-in to completely shut down all its activity.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Forcing a class to "die" ?

    Quote Originally Posted by CornedBee
    Killing threads is a bad idea.

    Just create an interface that all these classes have to implement. Give it a method shutdown() that requires the plug-in to completely shut down all its activity.
    He didn't create the classes as I guess (Doesn't have the source)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: Forcing a class to "die" ?

    After playing with threads a lot, I've resigned to using CornedBee's suggestion - the classes is still instanced somewhere in the VM, but it isn't bothering me anymore!
    ^_^

Posting Permissions

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



Click Here to Expand Forum to Full Width