Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Apr 30, 1998

Garbage collection process

Question:
If i have a code like this in the while loop
 while (true){
      java.util.date ldt_date = new java.util.Date()
      ...
}
Will the ldt_date variable be initialized with new location in memory all the time and previously allocated will be garbage collected by java. I am not sure about what's happening in here. Could you explain me more about the behaviour of this statement.

Answer:
Every time your example loop executes, a new java.util.Date instance is created, located at a different place in the heap than the previously created instance. If no variables reference the previously created Dates, then those data structures become eligible for garbage collection. The general rule is that every time you use the new operator to create an object, new memory will be allocated and whenever a piece of memory is no longer reachable by tracing references from active data structures in a program, that memory becomes eligible for garbage collection. However, exactly when and how memory is garbage collected is system dependent.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap