首页 \ 问答 \ 调用System.gc()后,WeakReferenced对象不是垃圾回收(WeakReferenced object is not garbage collected after calling System.gc())

调用System.gc()后,WeakReferenced对象不是垃圾回收(WeakReferenced object is not garbage collected after calling System.gc())

我是Java的新学习者。 我现在正在学习WeakReference的概念。 我遇到了一个可能看起来很愚蠢的问题,但我只是想弄清楚原因。 问题是:根据Java doc,“弱引用对象,它们不会阻止它们的引用被最终化,最终化,然后被回收。”

所以我做了这个小测试:

import java.lang.ref.WeakReference; 

public class A {
    public static void main(String[] args) {
        A a = new A();
        WeakReference<A> wr = new WeakReference<>(a);
        a = null;

        A a1 = wr.get();

        System.out.println(a);
        System.out.println(a1);

        try {
            System.gc();

            Thread.sleep(10000);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(a1);
    }

    @Override
    protected void finalize( ) {
        System.out.println(Thread.currentThread().getName() + ": See ya, nerds!");
    }
}

但是,我注意到在GC运行之后, wr.get()仍然可以返回我期望为null的对象,并且没有调用方法finalize() 。 出了什么问题? 感谢您的帮助! :)


I am a fresh new learner of Java. I'm now learning the concept of WeakReference. I came across a problem which probably looks stupid but I just wanna figure out the reason. The problem is: according to Java doc, "Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed."

So I did this small test:

import java.lang.ref.WeakReference; 

public class A {
    public static void main(String[] args) {
        A a = new A();
        WeakReference<A> wr = new WeakReference<>(a);
        a = null;

        A a1 = wr.get();

        System.out.println(a);
        System.out.println(a1);

        try {
            System.gc();

            Thread.sleep(10000);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(a1);
    }

    @Override
    protected void finalize( ) {
        System.out.println(Thread.currentThread().getName() + ": See ya, nerds!");
    }
}

However, I noticed that after GC running, wr.get() could still return object which I expected null, and the method finalize() was not invoked. So what went wrong? Thanks for your kind help in advance! :)


原文:https://stackoverflow.com/questions/50718490
更新时间:2023-05-01 13:05

最满意答案

您应该从DateTimeKind.Local切换到DateTimeKind.Utc以避免像这样的惊喜。

小时差异源于您的服务器位于与模拟器不同的时区。

通常,您应该在存储之前将本地时间转换为Utc时间,然后在显示之前立即将存储的Utc时间转换为本地时间。 并习惯于在数据库的原始数据中看到Utc时间。 :-)


You should switch from DateTimeKind.Local to DateTimeKind.Utc to avoid surprises like this one.

The hour difference stems from your server being in a different time zone than your emulator.

In general you should convert local times to Utc times before storing, and then convert the stored Utc times to local time immediately before displaying them. And get accustomed to seeing Utc times in your database's raw data. :-)

相关问答

更多
  • 我有同样的问题,我按照汤姆先生的建议做了所有事情。 但仍然镀铬报告没有Access-control-allow-origin标头存在..在用fidler检查后我意识到我的请求通过代理服务器而我的代理服务器正在处理预检选项请求.. 所以在“互联网选项”中,我删除了代理服务器,发现一切正常 ...... !!! The root of the problem The Token action is not hosted in a controller but is instead built in somewh ...
  • 您是否考虑过这一点,我在同一条船上,并认为它将解决我的问题。 但要回答您的问题,它似乎可以在自托管环境中使用。 Owin.UrlRewrite 编辑 - 这个库不起作用(至少我肯定无法使它工作)。 我确实尝试过我自己的OWIN中间件,它可以通过重定向完成,但URL中的闪烁实际上是hashtag url(在客户端路由器启动之前(在我的情况下是Aurelia)并使其成为非哈希URL)。 我可以说最好的是这是一个有限的用例(需要URL重写的自托管),因为我找不到预先制定的解决方案来做到这一点。 Have you ...
  • 您应该从DateTimeKind.Local切换到DateTimeKind.Utc以避免像这样的惊喜。 小时差异源于您的服务器位于与模拟器不同的时区。 通常,您应该在存储之前将本地时间转换为Utc时间,然后在显示之前立即将存储的Utc时间转换为本地时间。 并习惯于在数据库的原始数据中看到Utc时间。 :-) You should switch from DateTimeKind.Local to DateTimeKind.Utc to avoid surprises like this one. The h ...
  • 听起来您的VS2012实例已从服务主机中脱机。 尝试像这样更改配置...
    我假设你已经设置了gitlab来使用LFS。 信息在这里: https : //docs.gitlab.com/ee/workflow/lfs/manage_large_binaries_with_git_lfs.html 是否有办法在进口中包含LFS,我不知道; 但既然你已经完成了核心回购的导入,那么也不用担心。 您可以使用本地仓库作为中间人将所有LFS对象都放入gitlab。 由于涉及大量数据,这可能是耗时的。 在原始仓库的克隆中,您将执行git lfs fetch --all (记录为“主要用于备份或 ...
  • Harry Pierson(DevHawk)有一篇很好的博客文章,可以帮助您开始进行DLR调试。 Microsoft.Scripting.Debugging Harry Pierson (DevHawk) has a good blog post to get you started with DLR debugging. Microsoft.Scripting.Debugging
  • 您必须使用.net中的SQLBulkCopy或发送XML以在SQL代码中进行解析 After some trial, I was able to get a solution myself. Here are the solution steps (Due to client confidentiality, I cannot share the code): I read all the text from the file on the Web Server from the ASP code usin ...
  • Subversion与TortoiseSVN和Team Foundation Server都会发现非常相似的功能,尽管TFS可以更好地集成到Visual Studio中。 没有任何杀手级功能或问题会导致我建议从TFS切换到Subversion,如果有什么TFS的集成特性会让我建议切换到TFS。 TFS还具有内置任务/错误跟踪的优势,因此您可以在一个开发工具中拥有所有内容。 You will find very similar features in both Subversion with Tortoise ...
  • 托管和自由环境与实现之间的区别在于C标准库的存在。 语言功能本身可供两者使用(少数可选功能除外)。 但是如果一个环境被称为“托管”,它必须提供标准所要求的完整(库)功能。 没有中途。 为托管环境编写的AC程序可以期望存在所有必需的特征,因此可以使用malloc等。一旦缺少这样的特征,例如动态内存分配,它不是托管环境,而是独立的。 这并不意味着后者无法提供某些标准库(或其某些功能)。 实际上,即使对于独立环境,也可能存在例如可用的转换功能。 因此,如果没有可用的标准库,程序如何运作? 简单地说:它不使用这些功 ...
  • 我会改进日期选择器,以便它回发的值始终是yyyy-MM-dd,但显示在你关心的任何文化中。 这使问题成为客户端问题,而不是服务器问题(它确实是) UPDATE 我做了一些调查Convert.ToDateTime()只是使用当前的文化设置调用DateTime.Parse。 我刚刚检查了我的一个虚拟机,它运行的是美国本地设置,这就是为什么你的d / M / y错误。 我会对发送到服务器的日期的格式进行标准化(对于哪种格式并不重要,但我始终是YMD的粉丝)然后使用@No One提到的DateTime.TryPar ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。