Author Topic: Garbage Collector  (Read 6243 times)

0 Members and 1 Guest are viewing this topic.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Garbage Collector
« on: August 04, 2008, 04:43:39 pm »
I am looking for a way to make my huge JavaOP2 plugin (and JavaOP2 overall, without modifying source) more efficient. I do not know exactly how the Garbage Collector works and when to invoke it. Can someone shed some light on this for me? Thanks.
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Garbage Collector
« Reply #1 on: August 04, 2008, 04:46:56 pm »
The garbage collector deletes objects that have no references to them. It should invoke itself on a regular basis, you probably don't need to invoke it.

To make your object get garbage collected, just delete any reference to it. As in, set the object to null, make sure it isn't in any lists, etc.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: Garbage Collector
« Reply #2 on: August 04, 2008, 05:02:40 pm »
Ah, thats how I usually unset my variables.

Thanks!
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: Garbage Collector
« Reply #3 on: August 04, 2008, 08:20:16 pm »
Out of curiosity, what criteria are you using to decide JavaOp needs to be more efficient?  I ask because you wouldn't be the first person to mistake flood protection for program slowness.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Garbage Collector
« Reply #4 on: August 05, 2008, 12:11:13 am »
The point of a GC is to take the stress of resource management largely off of the user.
Objects are released when a) You exit their scope, b) All references are removed or c) (not sure if this is possible in Java) explicitly invoke the GC.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: Garbage Collector
« Reply #5 on: August 05, 2008, 12:15:50 am »
This article provides an excellent read if you are interested in how the java GC works and optimizing it:

http://www.ibm.com/developerworks/java/library/j-jtp01274.html

However, it's an old article and I don't know how much of it pertains to more recent java verisons.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Garbage Collector
« Reply #6 on: August 06, 2008, 09:18:24 pm »
The point of a GC is to take the stress of resource management largely off of the user.
Objects are released when a) You exit their scope, b) All references are removed or c) (not sure if this is possible in Java) explicitly invoke the GC.

Not exactly. Firstly, A is just a special case of B. Secondly, C doesn't allow you to destroy a strongly referenced object. Lastly, that list is incomplete: there are four levels of reference strength:
* Phantom
* Weak
* Soft
* Strong

If there were only strong references, your B would be the only reason an object could become a candidate for GC. A strong reference is a direct reference; given:
Code: [Select]
Object o = new Object();o is a strong reference to the new object, and that object can therefore not be a candidate for GC until o's scope closes, or possibly longer if the object becomes referenced by anything else.

Then you have 3 classes that built off of Refrence. Think of the reference class as such:
Code: [Select]
public abstract class Reference<T> {
    T ref;

    public Reference<T>(T ref) {
        this.ref = ref;
    }

    public T get() {
        return ref;
    }
}

The phantom reference never stores a copy of the object:
Code: [Select]
public class PhantomReference<T> extends Reference<T> {
    public PhantomReference<T>(T ref) {
        super(null);
    }
}

Then you have soft/weak references, which behave out of the ordinary with regards to the GC. These two types of references are only guaranteed to reference an object for as long as that object is strongly reachable. In the case of a weak reference, the object will be destroyed upon the first GC cycle. In the case of a soft reference, the GC will try to keep it around until it runs out of memory.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: Garbage Collector
« Reply #7 on: August 11, 2008, 11:56:01 pm »
Out of curiosity, what criteria are you using to decide JavaOp needs to be more efficient?  I ask because you wouldn't be the first person to mistake flood protection for program slowness.

I want to lower the amount of memory JavaOP2 uses since it is temporarily on a low-memory windows system.
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: Garbage Collector
« Reply #8 on: August 12, 2008, 12:36:39 am »
Turn off the GUI Plugin then :)

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: Garbage Collector
« Reply #9 on: August 12, 2008, 10:16:47 am »
Turn off the GUI Plugin then :)

For some reason, JavaOP2 is using more memory without the GUI plugin :(
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Garbage Collector
« Reply #10 on: August 12, 2008, 08:49:42 pm »
You are probably using the Server GC profile. Add -client to the java command line.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: Garbage Collector
« Reply #11 on: August 13, 2008, 04:11:45 pm »
I have more memory available now, so never mind. Thanks for all the help  :)
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!