GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Do not call finalize() — CodeQL query help documentation

Do not call finalize()¶

ID: java/do-not-call-finalize
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
   - performance
   - external/cwe/cwe-586
Query suites:
   - java-code-quality.qls

Click to see the query in the CodeQL repository

Overview¶

Triggering garbage collection by directly calling finalize() may either have no effect or trigger unnecessary garbage collection, leading to erratic behavior, performance issues, or deadlock.

Recommendation¶

Avoid calling finalize() in application code. Allow the JVM to determine a garbage collection schedule instead. If you need to explicitly release resources, provide a specific method to do so, such as by implementing the AutoCloseable interface and overriding its close method. You can then use a try-with-resources block to ensure that the resource is closed.

Example¶

Incorrect Usage¶

class LocalCache {
    private Collection<File> cacheFiles = ...;
    // ...
}

void main() {
    LocalCache cache = new LocalCache();
    // ...
    cache.finalize(); // NON_COMPLIANT
}

Correct Usage¶

import java.lang.AutoCloseable;
import java.lang.Override;

class LocalCache implements AutoCloseable {
    private Collection<File> cacheFiles = ...;
    // ...

    @Override
    public void close() throws Exception {
        // release resources here if required
    }
}

void main() {
    // COMPLIANT: uses try-with-resources to ensure that
    // a resource implementing AutoCloseable is closed.
    try (LocalCache cache = new LocalCache()) {
        // ...
    }
}

Implementation Notes¶

This rule ignores super.finalize() calls that occur within finalize() overrides since calling the superclass finalizer is required when overriding finalize(). Also, although overriding finalize() is not recommended, this rule only alerts on direct calls to finalize() and does not alert on method declarations overriding finalize().

References¶

  • SEI CERT Oracle Coding Standard for Java: MET12-J. Do not use finalizers.

  • Java API Specification: Object.finalize().

  • Java API Specification: Interface AutoCloseable.

  • Java SE Documentation: The try-with-resources Statement.

  • Common Weakness Enumeration: CWE-586.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.