如果引用永远不会被使用,那么持有对象的引用是否会将其保存在内存中? [英] Does holding a reference to an object will keep it in memory if the reference will never be used?

查看:148
本文介绍了如果引用永远不会被使用,那么持有对象的引用是否会将其保存在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WeakReferences收集对象。
对象在运行时被动态加载,并且可以通过GC随机收集。

偶尔,我需要永久删除一个对象,这是通过异步DB命令。
含义 - 删除命令和持久性数据库中的实际提交删除之间有一段时间,对象可以被GCed,然后从DB重新加载。



解决这个问题的最简单方法是在每个对象中都有一个'Deleted'标志(已经有一个),并且在执行删除操作时,将强引用删除的对象添加到DB Command对象,所以当操作完成时,强引用丢失,对象可以永久释放。



现在的问题是,GC是否允许我的对象以只读方式先?它在这种情况下适用吗?
我是否确保在DB命令出列并处理之前不会收集该对象?



以下是一些较低级别的详细信息:

  Dictionary< Int64,WeakReference> mItems; 
struct IOCommand
{
public CommandDetails命令;
public object [] strongReferences;
}
队列< IOCommand> mIOQueue;
void queueCommand(CommandDetails命令,object relatedObject)
{...}

你可以看到,当排队一个命令时,我将一个相关对象作为一个强引用保存在队列中,直到它被处理,但是由于没有使用该对象,问题是它们可以被优化掉...



Amit。

解决方案

只要您的代码有一个(非弱)对象不会被收集。

所以你应该是安全的,(未来的)Db Command会使用这个引用确保它仍然存在。

只要确保在这种情况下您不依赖WeakReference,那么强大的参考应该有适当的寿命。





但是由于对象没有被使用,所以问题是可以将它们优化掉。 。


没有。只要命令存储在队列中,并且只要队列可以到达,那么存储它们的方式将确保它们存在。



但请注意,如果它们确实没有被使用,它甚至不应该...


I have a collection with WeakReferences to object. The object are loaded dynamically during runtime, and can be collected by GC randomly.

Occasionally, I need to permanently delete an object, this is done with asynchronous DB command. Meaning - there is a time period between the delete command and the actual commiting of the deletion in the persistency database where the object can be GCed, and then reloaded from the DB.

The easiest way for me to solve this would be to have a 'Deleted' flag in each object (already have one), and when performing the deletion action, add a strong reference to the deleted object to the DB Command object, so when the operation completes the strong reference is lost and the object can be released forever.

The question is, is the GC allowed to collect my object with read-ahead? does it apply in this scenario? Am i guaranteed the that object will not be collected until the DB command is dequeued and processed?

Here is some lower level details:

Dictionary<Int64, WeakReference> mItems;
struct IOCommand
{
    public CommandDetails command;
    public object[] strongReferences;
}
Queue<IOCommand> mIOQueue;
void queueCommand(CommandDetails command, object relatedObject)
{...}

You can see that when queuing a command I pass a related object as a strong reference kept in the queue until it is processed, but as the object are not used the question is can they be optimized away...

Amit.

解决方案

As long as your code has a (non-weak) reference to an object it will not be collected.

So you should be safe, the very fact that a (future) Db Command will use the reference makes sure it will still be be there.

Just make sure you don't rely on a WeakReference in this case, that strong reference should have an appropriate lifespan.


but as the object are not used the question is can they be optimized away...

No. The way you store them will make sure they exist as long as the command is stored in the Queue, and as long as the queue is reachable of course.

But note that if they are really not used it shouldn't even matter...

这篇关于如果引用永远不会被使用,那么持有对象的引用是否会将其保存在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆