object without reference and gc()

A

asit

Consider the following code

class NewThread implements Runnable {

Thread t;

NewThread(String name) {
t = new Thread(this,name);
System.out.println("New thread : " + t);
t.start();
}

public void run() {
try {
for(int i=0; i<5; i++) {
System.out.println(this + " : " + i);
Thread.sleep(500);
}
}catch(InterruptedException e) {
System.out.println("Child interrupted");
}
System.out.println("Child thread exiting");
}
}

public class MyThread {

public static void main(String args[] ) {

Runtime r = Runtime.getRuntime();
new NewThread("Ok1");
new NewThread("Ok2");
new NewThread("Ok3");

r.gc();

try {
Thread.sleep(1000);
}catch(InterruptedException e) {
System.out.println("Main thread interrupted");
}

System.out.println("Main thread exiting");
}

}

In MyThread class, inside main(), I am creating three objects of
NewThread class without storing there references. Hence when I am
calling garbage collector, all these objects should be killed. But in
reality it is not happening. Can anyone help me ???
 
G

grendal

In MyThread class, inside main(), I am creating three objects of
NewThread class without storing there references. Hence when I am
calling garbage collector, all these objects should be killed. But in
reality it is not happening. Can anyone help me ???

In addition to what Patricia said, even calling gc() isn't going to
mean that your object will be garbage collected.
 
R

Ratnesh Maurya

In addition to what Patricia said, even calling gc() isn't going to
mean that your object will be garbage collected.

Yes Grendal,

I would like to rephrase what you said, calling gc() doesn't
guarantees that it will run the garbage collection. Its totally in
control of runtime.

Regards,
-Ratnesh
S7 Software
 
R

Roedy Green

In MyThread class, inside main(), I am creating three objects of
NewThread class without storing there references. Hence when I am
calling garbage collector, all these objects should be killed. But in
reality it is not happening. Can anyone help me ???

Threads don't get gced until they complete. The OS has a reference to
them. How do you know the dead thread objects are not being GCed?
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
 
A

Arne Vajhøj

Roedy said:
Threads don't get gced until they complete. The OS has a reference to
them. How do you know the dead thread objects are not being GCed?

The GC has no way of knowing that the OS refers to something.

But the JVM also has a ref - and the GC knows about that.

Arne
 
R

Roedy Green

The GC has no way of knowing that the OS refers to something.

But the JVM also has a ref - and the GC knows about that.

Correct. The object doing the pinning lives in the JVM. It represents
the OS native thread. I was speaking too loosely.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
 
T

Tris Orendorff

"It's ..."

.... Monty Python's Flying Cir-cus.


--
Tris Orendorff
[ Anyone naming their child should spend a few minutes checking rhyming slang and dodgy sounding names. Brad and
Angelina failed to do this when naming their kid Shiloh Pitt. At some point, someone at school is going to spoonerise her
name.
Craig Stark ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top