scenario: you have a bunch of threads.
each thread has a data string comma separated.
each thread creates an instance of the string parser class.
after parsing, it then creates an instance of a string manipulator class.
after this class manipulates the string, it creates an instance of a message sender class for that string.
thread (creates instance of class A), in class A an instance of class B is created, in class B an instance of class C is created.
for each thread creating 3 instances of 3 different classes, will all 3 instances be cleaned up by Java garbage collector upon the thread finishing it's task?
or should I be calling some cleanup/delete method?
I have to have instance of these classes or my threads will step all over each other.
The garbage collector will run (when it feels like it) after there are no more references to your objects.
When you say a thread creates an instance of class A, I assume you mean an instance of a class that implements Runnable creates an instance of class A in its run() method?
If you store the reference in a method variable then the instance of class A is eligible for GC when the run method completes (goes out of scope) or when you set the reference variable to null. If class A has a reference to class B and Class B has a reference to class C then everything will be eligible for GC when class A goes out of scope.
If you store a reference to class A in a class member variable of the Runnable class then the instance will go out of scope when the Runnable class goes out of scope (or when you set the member variable reference to null). This implies that you must look at how you created the Runnable class and where (if at all) you stored references to it. Note that if you created a Thread instance and gave it your Runnable instance, the Thread instance probably has a reference to your runnable instance.
If you store the reference to A in a static member variable then it will go out of scope when the application ends or you set the static member variable to null.
Joined: Apr 09, 2010
so I think I understand what you're saying, and that it's will call the GC when the runnable class or thread finishes or goes out of scope.
thanks for your helpful post, here is the progression of code. there is lots of string manipultation but I just show for our purpose the class instantiation and thread creation.
basically my threads get created in class 1:
class 2's "run()" method which implements runnable:
(after doing what it needs to do, passes current column & data string off to parser object)
genericSoapDataParser is class A in the scenario I posted earlier
genericSoapMsgCreator is class B, after further creating a string
SoapServiceInvoker is class C, simply takes the string, which is an SOAP message, and call the web service
Lavanya Halliwell wrote:so I think I understand what you're saying, and that it's will call the GC when the runnable class or thread finishes or goes out of scope.
The class instance becomes eligible for garbage collection when there are no more references to it, but that doesn't mean it will be garbage collected. There are various different GC strategies available (you can set them on the command line), but there are no guarantees when or if an object will be GC'd. You can programmatically request a GC scan, but it's just that - a request; the JVM can ignore it. While there is memory available, garbage collection isn't a priority.