About finalize a problem! Please advise

Category: Java SE
2012-12-18 05:31:40

Sponsored Links
class Book {
boolean checkedOut = false;

Book (boolean checkOut) {
checkedOut = checkOut;
void ; checkIn () {
checkedOut = false;
; public void finalize () {
if (checkedOut)
; {
System.out.println ("Error1: checked stat:" + checkedOut);
System.out.println ("Error2: checked out:" + checkedOut);

public class TerminationCondition ; {
public static void main (String [] ; args) {
Book novel = new Book (true);
/ / Proper cleanup:
novel.checkIn ();
/ / Drop the reference, forget to clean up:

Book nove2 = new ; Book (true);
/ / Proper cleanup:
nove2.checkIn ();
/ / Drop the reference, forget to clean up:
nove2 = null;

new Book ( true); / / nove3
/ / ; Force garbage collection & finalization:
; System.gc ();

execution results are as follows:
Error2: checked out: false / / nove2 be recycled
Error1: checked ; stat: true / / nove3 be recycled
Quote from jackychen_king ( jacky) seniors!
ask if not System.gc (), finalize () function would not have to clean up? Will default to recover part of the garbage collection?
system.gc just tell the system to forcibly reclaim memory, but for a specific object, what time is determined by the system recovery. ask, are those that determine the value of the contents of it?
first timers, please exhibitions!

Sponsored Links

2012-12-18 06:01:36
Called by the garbage collector on an object when garbage collection determines that there are no ; more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
Recycling is an object in the end of its life cycle and finalize, but not directly related to the
an object's finalize method calls are regulated by the garbage collector

As the program three case nove1 is not performed because the object is no longer finalize the remaining two references
were called after the gc finalize
2012-12-18 06:25:40
This is not very clear, ah, your second is new out of the objects began to be cited, and later the reference has been given a null, is that the new object is disconnected, of course, was the object of the system considered invalid, and the third new out also because not referenced to, system that is no longer valid, so they will be recovered out
2012-12-18 06:54:43
That is, the system is already in the free state of objects recovered.
2012-12-18 07:22:44

look back!
2012-12-18 07:59:05

hard to see!
2012-12-18 08:29:31
see your jvm is how to achieve a,
2012-12-18 08:33:09
finalize best not to manually invoke
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.