wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java class instances question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java class instances question" Watch "Java class instances question" New topic
Author

Java class instances question

Lavanya Halliwell
Ranch Hand

Joined: Apr 09, 2010
Posts: 57
I just have a quick question for y'all.

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.

Lavanya
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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.
Lavanya Halliwell
Ranch Hand

Joined: Apr 09, 2010
Posts: 57
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
Dave Lorde
Greenhorn

Joined: Apr 02, 2007
Posts: 20
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.
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 57
Dave Lorde wrote: There are various different GC strategies available (you can set them on the command line).


Dave, correct me if I am wrong this is only from jdk1.4
Dave Lorde
Greenhorn

Joined: Apr 02, 2007
Posts: 20
Rajesh Nagaraju wrote:
Dave Lorde wrote: There are various different GC strategies available (you can set them on the command line).


Dave, correct me if I am wrong this is only from jdk1.4


I don't recall, but I doubt that many people are still developing with 1.3 or earlier versions.

This covers the GC options: Tuning Garbage Collection With Java 5
Lavanya Halliwell
Ranch Hand

Joined: Apr 09, 2010
Posts: 57
thanks again for the reply.

i'll have to put in some code to clean my many class instances, as that would be the smartest thing to do.
currently i'm using jdk 1.6.

Lavanya
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java class instances question
 
Similar Threads
does this sound good/okay/terrible?
Threads and Synchronization examples
What is this example trying to demonstrate ?
SINGLETON METHOD
Synchronization block