• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java class instances question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic