Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating multiple Date instances in a Thread and memory

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49382
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49382
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic