Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How do you know if something eligible for garbage collection will actually be garbage collected?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finalize method may be defined in the Main class, but it's also not called in the main method:

The function call is commented out on line 9, and yet I still get the output "Roar!". How is that possible?

Also, here's what the book (Jeanne Boyarsky and Scott Selikoff, OCA Java SE 8 Programmer I Study Guide, Chapter 1 review questions answer key, page 336) says about the garbage collector:
"Calling System.gc() suggest that Java might wish to run the garbage collector. Java is free to ignore the request. The finalize() method runs if an object attempts to be garbage collected."

I'm having a hard time understanding the sovereignty of the garbage collector. I thought the garbage collector was supposed to be automatic at the end of a program in Java, or is it?
 
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:The finalize method may be defined in the Main class . . .

No, it is defined in java.lang.Object. It is overridden in Main.

The function call is commented out on line 9, and yet I still get the output "Roar!". How is that possible?

Don't call it a function; it isn't a function becase it doesn't return anything. Besides, it is called a method in Java®.
You should not call finalize() directly; it is called by the JVM when garbage collection (=GC) occurs. That is why you are getting that output.

. . . Java is free to ignore the request. . . .

That is correct. You might not see that output.

the sovereignty of the garbage collector. I thought the garbage collector was supposed to be automatic at the end of a program in Java, or is it?

Yes, normally you wouldn't call System.gc() at all. In this case, this is a cert exam practice question, so you are allowed to ask something you wouldn't normally do. The GC process runs whenever the JVM runs low on memory. If you comment out line 11, there will probably not be any GC at all. It is usually not necessary for it to occur at the end of the program session.

Moving to our exam forum.
 
Marshal
Posts: 7185
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:How do you know if something eligible for garbage collection will actually be garbage collected?


You don't. You can tell whether object is eligible to be garbage collected, but you can't tell when or if at all will be. That is up for the GC to decide.
 
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the early days of Java, explicitly calling gc was a strategy because gc ran as a massive process that could impact performance of other threads. One of the best examples of this, in fact, was long before Java on one of the AmigaBASIC demo programs for the Commodore Amiga.

While that long-ago machine's standard features such as DMA, hardware graphics acceleration and built-in stereo are nothing unusual these days (although it was the only computer I ever bought that had Total Harmonic Distortion and Stereo Separation values listed on the box), they were unheard of back then, when VGA meant that an IBM PC had a high-quality graphics card installed, a SoundBlaster card (probably monophonic), and DMA was virtually unheard of.

In this perspective, then, the ABasiC demo displayed 4 windows on the screen with different animations in each window and played a cute little tune.

Except that every few minutes, the ABasiC garbage collector would kick in and the music would stall. And this, children, is why you wanted to control when and how garbage collection ran.

Modern JVM garbage collection systems are not so brutal. They tend to operate more incrementally and in ways that don't impact performance as much, which is why gc() is more advisory than absolute.

The ultimate garbage collection comes when the program exits and the JVM terminates, though. That doesn't run gc, because once the JVM ends, the entire block of memory it lived in is given back to the operating system as a unit, rather than eliminating objects one at a time.
 
Saloon Keeper
Posts: 10675
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reacting to the title of your topic: If you really want to be sure something was garbage collected, use the Cleaner or PhantomReference classes. Overriding finalize() is always a bad idea.

Note that using these classes will never force the garbage collector to run. You can only use them to detect that it was run.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!