finalize

Java基础之System类

余生长醉 提交于 2019-11-26 16:18:02
System类 主要用于:计算代码的执行时间      进行垃圾收集操作 public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)//实现数组拷贝。 src -源阵列。 srcPos -源数组中的起始位置。 dest -目标数组。 destPos 在目标数据的起始位置。 length -数组元素的数目被复制。 public static long currentTimeMillis()//取得当前系统时间,返回毫秒数 public static void gc()//垃圾回收操作,这个方法是间接调用了Runtime类中的gc()方法。一个类的对象的产生一定会调用构造方法,如果在这个对象要被回收时,连个过程都没有,观察不到。如果需要给对象一个收尾的操作,那么可以写Object类中的finalize()方法 完成。  protected void finalize() throws Throwable//在对象回收时,就算抛出了异常,也不会影响到整个程序的正常运行。示例:finalize()方法使用 1 package com.imooc.demo; 2 3 import java.io.IOException; 4 5 public class

finalize()方法详解

五迷三道 提交于 2019-11-26 13:28:03
前言 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法。。 finalize的作用 (1)finalize()与C++中的析构函数不是对应的。C++中的析构函数调用的时机是确定的(对象离开作用域或delete掉),但Java中的finalize的调用具有不确定性 (2)不建议用finalize方法完成“非内存资源”的清理工作,但建议用于:① 清理本地对象(通过JNI创建的对象);② 作为确保某些非内存资源(如Socket、文件等)释放的一个补充:在finalize方法中显式调用其他资源释放方法。其原因可见下文[finalize的问题 finalize的问题 (1)一些与finalize相关的方法,由于一些致命的缺陷,已经被废弃了,如System.runFinalizersOnExit()方法、Runtime.runFinalizersOnExit()方法 (2)System.gc()与System.runFinalization()方法增加了finalize方法执行的机会,但不可盲目依赖它们 (3)Java语言规范并不保证finalize方法会被及时地执行、而且根本不会保证它们会被执行 (4)finalize方法可能会带来性能问题。因为JVM通常在单独的低优先级线程中完成finalize的执行 (5

Difference between destructor, dispose and finalize method

自作多情 提交于 2019-11-26 10:18:39
问题 I am studying how garbage collector works in c#. I am confused over the use of Destructor , Dispose and Finalize methods. As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class. The Dispose method is meant to provide the user to control the garbage collection. The Finalize method frees the

Why is the finalize() method in java.lang.Object “protected”?

流过昼夜 提交于 2019-11-26 09:36:26
问题 Out of curiosity, Why is the finalize() method\'s access modifier is made as protected . Why cant it be public ? Can someone explain me any specific reason behind this? Also, I came to know that finalize() method is called only once. If I call it twice in my program internally, what is happening? Will the garbage collector call this again? private void dummyCall() { try { finalize(); finalize(); } catch (Throwable e) { e.printStackTrace();//NOT REACHES EXCEPTION } } 回答1: I answer your

走进JVM

拟墨画扇 提交于 2019-11-26 03:44:47
首先,需要明白JVM是什么? JVM(Java Virtual Machine的简称。意为Java虚拟机。).是通过软件模拟Java字节码的指令集,JVM中只是主要保留了PC寄存器,其他的寄存器都进行了裁剪 JVM是一台被定制过的现实当中不存在的计算机 可以说JVM是Java实现跨平台的核心,也是整个Java的核心之一,是Java程序运行的必备条件,下面就描述介绍一下有关JVM的若干东西。 JVM会在执行Java程序的过程中把它管理的内存划分为若干个不同的数据区域。这些数据区域各有各的用处,各有 各的创建与销毁时间,有的区域随着JVM进程的启动而存在,有的区域则依赖用户线程的启动和结束而创建与销 毁。 2. 垃圾回收与内存分配策略 1 如何判断对象已"死“ - 引用计数法 给对象增加一个引用计数器,每当有一个地方引用它时,计数器就+1;当引用失效时,计数器就-1;任何时刻计数 器为0的对象就是不能再被使用的,即对象已"死"。 此法有一个缺点:无法解决循环引用问题,他会导致无用对象仍然判断存活而无法被回收,导致内存泄漏 - 可达性分析法 通过一系列称为"GC Roots"的对象作为起始点,从这些节点开始向下搜索,搜索走过的路径 称之为"引用链",当一个对象到GC Roots没有任何的引用链相连时(从GC Roots到这个对象不可达)时,证明此对象 是不可用的。 那么哪些对象可以作为

根父类:Object 类

蓝咒 提交于 2019-11-25 23:47:48
一、Object类   Java中规定: 如果一个类没有显式声明它的父类(即没有写extends xx),那么默认这个类的父类就是java.lang.Object。   类 Object 是类层次结构的 根类 。每个类都使用 Object 作为超类。    如何理解根父类?     (1)所有对象(包括数组)都实现这个类的方法,即Object类中声明的方法,所有引用数据类型(包括数组)中都有。     (2)所有类的对象的实例化过程,都会调用 Object 的实例初始化方法。     (3)所有的对象都可以赋值给 Object 的变量,或者说 Object 类型的变量,形参,数组可以接受任意类型的对象。 二、常用方法   1、toString() 方法 public String toString()      用于返回对象的信息,建议所有的子类都重写。如果没有重写:返回的字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。     如果直接打印一个对象,或者用对象与字符串进行拼接,默认情况下自动调用这个对象的 toString() 方法。   2、getClass() 方法 public final Class getClass()      该方法返回此 Object 的运行时类。(分为编译时类型与运行时类型)   3

finalize() called on strongly reachable objects in Java 8

二次信任 提交于 2019-11-25 23:35:30
问题 We recently upgraded our message processing application from Java 7 to Java 8. Since the upgrade, we get an occasional exception that a stream has been closed while it is being read from. Logging shows that the finalizer thread is calling finalize() on the object that holds the stream (which in turn closes the stream). The basic outline of the code is as follows: MIMEWriter writer = new MIMEWriter( out ); in = new InflaterInputStream( databaseBlobInputStream ); MIMEBodyPart attachmentPart =

Java面试 - final、finally、finalize的区别?

你说的曾经没有我的故事 提交于 2019-11-25 22:58:50
final :用于声明属性, 方法和类,分别表示属性不可变、方法不可覆盖、被其修饰的类不可继承。 finally :异常处理语句结构的一部分,表示总是执行。 finalize :Object 类的一个方法,在垃圾回收器执行的时候会调用被回收对象的finalize()方法。JVM不保证此方法总被调用。 举例: 修改被final修饰的属性nickName public class Student{ public static void main(String[] args) { final String nickName ="Jack"; nickName = "Jack Ma"; } } 运行结果 Error:(27, 5) java: 无法为最终变量nickName分配值 由此说明被final 修饰的属性是不可变的。 那么,final 修饰的方法setName()是否可以被重写呢? class Person { private String name; public final void setName(String name){ this.name = name; } } public class Student extends Person{ // 重写被final 修饰的setName()方法 public void setName(String name){ System

When is the finalize() method called in Java?

五迷三道 提交于 2019-11-25 22:18:58
问题 I need to know when the finalize() method is called in the JVM . I created a test class which writes into a file when the finalize() method is called by overriding it. It is not executed. Can anybody tell me the reason why it is not executing? 回答1: In general it's best not to rely on finalize() to do any cleaning up etc. According to the Javadoc (which it would be worth reading), it is: Called by the garbage collector on an object when garbage collection determines that there are no more