我如何明确清除字节[] [英] How do i explicitly clear the byte[]

查看:149
本文介绍了我如何明确清除字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建新的字节数组,这些字节数组并不是由GC收集的,而是生活在内存中并增加了专用字节。下面的代码每10秒执行一次。

I am creating new byte arrays which are not being collected by GC and are living in memory and increasing the private bytes. The code below gets executed every 10 seconds. How do I explicitly clear the variable after I am done with it?

byte[] outputMessage = new byte[10000];
//Do some work here


推荐答案

你知道他们没有被收集吗?您提供的代码很好,如果您没有任何悬挂引用,它应该有资格收集。

How do you know they are not being collected? The code you provide is fine, and it should become eligible for collection if you don't have any dangling references to it.

显式地,您可以通过

outputMessage = null;

,你可以提示GC应该用它做这件事:

and you can hint the GC that it should do its job with this:

GC.Collect();

然而,不能保证你的对象会被收集,手动搞乱GC通常不会需要。

However, there's no guarantee that your object will get collected, and manually messing with the GC is usually not needed.

如果您一直添加新的事件处理程序,您是否删除旧的处理程序?如果没有,那么他们仍然保留你的参考。

If you are adding a new event handler all the time, are you deleting any old ones? If not, then they are still keeping the reference for you.

编辑:为清晰起见,并由OP新的信息

for clarity, and for new info by OP

这篇关于我如何明确清除字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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