zoukankan      html  css  js  c++  java
  • Java Garbage Collection

    reference: http://www.studytonight.com/java/garbage-collection.php

    Garbage Collection

    In Java destruction of object from memory is done automatically by the JVM. When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object are released. This technique is calledGarbage Collection. This is accomplished by the JVM.

    Can the Garbage Collection be forced explicitly ?

    No, the Garbage Collection can not be forced explicitly. We may request JVM for garbage collection by calling System.gc() method. But This does not guarantee that JVM will perform the garbage collection.

    finalize() method

    Sometime an object will need to perform some specific task beofre it is destroyed such as closing an open connecton or releasing any resources held. To handle such situation finalize() method is used. finalize() method is called by garbage collection thread before collection object. It is the last chance for any object to perform cleanup utility.

    protected void finalize(){

    }

    1. finalize() method is defined in java.lang.Object class, therefore it is avaliable to all the classes.

    2. finalize() is decalred as proteced inside Object class

    3. finalize() method gets called onlu once by GC thread

    gc() Method

    gc() method is used to call garbage collector explicitly. However gc() method does not gurantee that JVM will perform the garbage collection. It request the JVM for garbage collection. This method is present in System and Runtime class.

    public class Test
    {
        
        public static void main(String[] args)
        {
            Test t = new Test();
            t=null;
            System.gc();
        }
        public void finalize()
        {
            System.out.println("Garbage Collected");
        }
    }



  • 相关阅读:
    PCL:描述三维离散点的ROPS特征(Code)
    veket智能机器人
    三维重建:SLAM的粒度和工程化问题
    DNN:逻辑回归与 SoftMax 回归方法
    人工智能:一种现代方法 第四版 翻译序言
    编程低级错误记录
    apache服务器配置防盗链(centos7)
    Linux下命令行中的复制和粘贴
    rm: cannot remove `libtoolT’: No such file or directory
    switch范围判断
  • 原文地址:https://www.cnblogs.com/morningdew/p/5618575.html
Copyright © 2011-2022 走看看