When is a Java local variable eligible for GC?

前端 未结 6 1463
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 01:11

Given the following program:

import java.io.*;
import java.util.*;

public class GCTest {

    public static void main(String[] args) throws Exception {
             


        
6条回答
  •  灰色年华
    2021-01-06 01:37

    As test is only used once, it can be removed immediately after the call to it. Even if the each call to read used a call to getInputStream instead of using the local is variable, use of the object could be optimised away. FIleInputStream cannot be finalised prematurely due to its use of locking. Finalisers are difficult.

    In any case, your finaliser is pointless. The underlying FileInputStream will close itself on finalisation anyway.

提交回复
热议问题