• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Garbage Collection  RSS feed

 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Consider line 14 and 16

when does garbage collector start?

1. when i assigned obj to null
2. or when i try to use obj in the method doSomething
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's up to the JVM when to run the GC; there's no guarantee when it'll happen. Since this code terminates quickly, it will probably never run.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:It's up to the JVM when to run the GC; there's no guarantee when it'll happen. Since this code terminates quickly, it will probably never run.


From what i know Unreferenced objects are deleted when the eden space is cleared.Also when an object is assigned null it triggered the "gc". But what i don't know is when does it happened?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, there's no guarantee when that happens. There's no point in doing a GC if plenty of memory is available.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:As I said, there's no guarantee when that happens. There's no point in doing a GC if plenty of memory is available.


Yeah you are right. It seem i can't force the jvm to gc. The closest i can get is to suggest a System.gc(). But that also is a suggestion there no guarantee it will happen.

It is a dead end
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:It is a dead end

Actually, it's a dead-end for you, not for your program. And the reason is that Java (or the JVM) doesn't WAN'T you to be managing its memory.

If you really need to manage memory, then you want a language like C or C++ - and believe me, you'll spend a LOT of time on it, because proper memory management is NOT simple. The designers of Java, rightly or wrongly, decided that most people probably don't need it, so they've made it impossible for you to decide how and when it will happen.

Note that that's not the same as saying that it WON'T happen; simply that you can't predict it. A Java program will not fail due to lack of memory if there is space that can be reclaimed by the garbage-collector.

HIH

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!