Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Core Java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java explitly how we call a gc() on which,like System.gc()or

runtime.gc() ?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
lowercase baba
Posts: 13001
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic