Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:56:42+00:00 2026-05-26T09:56:42+00:00

PS: I do know how to cleanup correctly, without depending on finalize() . Does

  • 0

PS: I do know how to cleanup correctly, without depending on finalize().

Does Java not guarantee that on program exit, proper garbage collection will be done?

E.g. lets say I’ve kept some data in cache instead of serializing it frequently, I also implemented finalize() with the hope that if due to whatever reason (except crash) my program exits gracefully, then the cache would be written to DB/file/some-storage by my code in finalize() method. But according to following little experiment it seems like JVM doesn’t cleanup memory “gracefully”, it just exits.

Java spec (see program exit) says NOTHING abt how memory / gc is handled on exit. Or should I have been looking at a different section of the spec?

Take the following example (output at the end) using 1.6.0.27 64 bits, on Windows 7

public class Main {

        // just so GC might feel there is something to free..
    private int[] intarr = new int[10000];

    public static void main(String[] args) {
        System.out.println("entry");
        Main m = new Main();
        m.foo();
        m = new Main();
        // System.gc();
        m.foo();
        m = null;
        // System.gc();
        System.out.println("before System.exit(0);");
        System.exit(0);
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("finalize()");
        super.finalize();
    }

    public void foo() { System.out.println("foo()"); }
}

/*
 * Prints:
 * entry 
 * foo() 
 * foo() 
 * before System.exit(0);
 */

Variations:

  • If I uncomment any one System.gc() then no finalize() is called.
  • If I uncomment both System.gc() then finalize() is called twice.
  • Whether System.exit() is called or not has no effect on whether finalize() is called or not.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:56:43+00:00Added an answer on May 26, 2026 at 9:56 am

    No, Java does not guarantee that on program exit the GC will trigger. If you want an operation to peform on exit use Runtime.addShutdownHook method. Read this Sun article on what the SPEC says.

    The specification for the Java platform makes very few promises about
    how garbage collection actually works. Here is what the Java Virtual
    Machine Specification (JVMS) has to say about memory management.

    The heap is created on virtual machine start-up. Heap storage for
    objects is reclaimed by an automatic storage management system (known
    as a garbage collector); objects are never explicitly deallocated. The
    Java virtual machine assumes no particular type of automatic storage
    management system, and the storage management technique may be chosen
    according to the implementor’s system requirements.1 While it can seem
    confusing, the fact that the garbage collection model is not rigidly
    defined is actually important and useful-a rigidly defined garbage
    collection model might be impossible to implement on all platforms.
    Similarly, it might preclude useful optimizations and hurt the
    performance of the platform in the long term.

    Although there is no one place that contains a full definition of
    required garbage collector behavior, much of the GC model is
    implicitly specified through a number of sections in the Java Language
    Specification and JVMS. While there are no guarantees about the exact
    process followed, all compliant virtual machines share the basic
    object lifecycle described in this chapter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Know of an OCAML/CAML IDE? Especially one that runs on Linux?
I know that I can do something like $int = (int)99; //(int) has a
It is pure winsock server question. Just to clear that I already know how
I'm doing some architectural cleanup that involves moving a bunch of classes into different
I'm still quite new with jQuery, I know that I could google and have
I'm using a simple program that will take tallPDF xml and convert it into
One of the first things I've learned about Java EE development is that I
I am currently working on a mantenance project that is written in Java. We
Some information: i have to write a program that controls some devices (this is
Know of any good libraries for this? I did some searches and didn't come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.