• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Core Java

 
ramesh vadugu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java explitly how we call a gc() on which,like System.gc()or

runtime.gc() ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't explicity call the garbage collector from Java code. The method System.gc() only suggests to the JVM it should run the garbage collector. It may not actually run.

A more important question to ask is not how you do this, but why you need to write code to attempt to manipulate how the JVM behaves after Java has been so carefully designed to avoid you having to do this sort of thing.
[ April 24, 2007: Message edited by: Paul Sturrock ]
 
James A Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
You can't explicity call the garbage collector from Java code. The method System.gc() only suggests to the JVM it should run the garbage collector. It may not actually run.

A more important question to ask is not how you do this, but why you need to write code to attempt to manipulate how the JVM behaves after Java has been so carefully designed to avoid you having to do this sort of thing.

[ April 24, 2007: Message edited by: Paul Sturrock ]


I would think a time you would want to explicitly invoke garbage collection would be when you know you are finished using large resource/resources and want garbage collection to occur as soon as possible to free them (the resources).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what does that gain you? if the JVM doesn't need the resources, then forcing a GC just wastes time. if the JVM does need the resources, it's going to do a GC anyway.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fred Rosenberger:
but what does that gain you? if the JVM doesn't need the resources, then forcing a GC just wastes time. if the JVM does need the resources, it's going to do a GC anyway.


If your program is in an "idle" state and you have freed up a large amount of objects, then it may be of benefit to suggest that the GC runs to dispose of the freed objects, so that when the next "busy" period arrives you do not have to wait around for a time-consuming GC to take place. In that way you could possibly get a more responsive system.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be right. I am always aware that I am unlikely to be able to successfully second guess what is best for the JVM to do at any current point in time. In the example you give my suspicions would be that the garbage collector may well run anyway during this idle state. However, since this is the beginner's forum I'd suggest the best strategy to follow is not to try to interact with the GC directly (not that you can) and if you are trying to stop and ask yourself "am I complicating this code with an unnecessary early optimization"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic