• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Garbage Collection

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic