The Calendar and Date objects become eligible for GC as soon as execution leaves the block in which they're created (in line 10). Thus the amount of memory needed stays constant.

(And even if they were not GC-ed, you would have to wait a very long time before creating 2 small objects per second became a problem, given the amount of memory a JVM has to work with.)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41065

29

posted Nov 13, 2013 08:55:32

1

Actually, no Date of Calendar objects become eligible. Since it says new Thread(), an ordinary Thread is created and started. Not an instance of class A.
It has an empty run() method, so it simply returns and the run () method shown is never called.

Campbell Ritchie wrote:Actually, no Date of Calendar objects become eligible. Since it says new Thread(), an ordinary Thread is created and started. Not an instance of class A.
It has an empty run() method, so it simply returns and the run () method shown is never called.

Even if I change the instance to class A which is extending class Thread then also program is running fine and run method is getting called why run method would not be called I am not getting this kindly help on this.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41065

29

posted Nov 13, 2013 10:25:14

1

As Ulf said earlier, at the end of the loop each Date object goes out of scope and becomes eligible for garbage collection. So you will never exhaust your available memory.