aspose file tools*
The moose likes Java in General and the fly likes Creating multiple Date instances in a Thread and memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating multiple Date instances in a Thread and memory" Watch "Creating multiple Date instances in a Thread and memory" New topic
Author

Creating multiple Date instances in a Thread and memory

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73


My concern is that should I not get memory out of error as after 1 second new calendar instance is getting created
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Please post correctly‑indented code; that is difficult to read. And it won't compile. If you ever get the code to compile, what happens when you run it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
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.)


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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.
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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: 39409
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating multiple Date instances in a Thread and memory