我如何估计调用System.gc()留下的内存量? [英] How can I estimate amount of memory left with calling System.gc()?

查看:120
本文介绍了我如何估计调用System.gc()留下的内存量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用以下配方的数据处理代码:

I have some data processing code which uses the following recipe:

  • 读入尽可能多的数据(称为块")
  • 对块执行处理
  • 将已处理的块写入磁盘
  • 重复
  • ...
  • 合并所有已处理的块以获得最终答案.

最后一个阶段在有尽可能少的块时是最有效的,因此我希望第一阶段读取尽可能多的数据以适合内存.我可以通过查询 Runtime.freeMemory() .

This last stage is most efficient when there are as few chunks as possible, so I want the first stage to read in as much data as will fit in memory. I can do this by querying Runtime.freeMemory().

但是,这意味着我需要调用

However, this means I need to call System.gc(), or the number returned by Runtime.freeMemory() is much smaller than the amount of memory I could safely allocate.

我听过许多权威人士说,明确地呼叫System.gc()是个坏主意.有什么办法可以避免这种情况吗?

I have heard a number of authorities say that calling System.gc() explicitly is a bad idea. Is there any way I can avoid this?

推荐答案

即使您在检查有多少内存之前立即调用System.gc(),也无法保证实际上会发生垃圾回收.我自己真的不会打扰,我会设置固定的块大小(最好通过属性或类似属性配置),并始终使用该大小.如果程序的其余部分足够简单,则可以仅使用块大小加上固定数量的兆字节作为堆大小.如果由于其他原因您的程序大小不确定,您可以考虑并排运行两个程序并使用IPC机制.

Even if you call System.gc() right before checking how much memory you have, there is no guarantee that a garbage collection will actually have occurred. Myself I really wouldn't bother, I'd set a fixed chunk size (preferably configured through a property or similar) and always use that. If the rest of your program is simple enough, you can just use the chunk size plus a fixed number of megs as the heap size. If the size of your program is too uncertain due to other reasons, you could look into running two programs side by side and using an IPC mechanism.

当然很可能是您的代码需要对内存进行更细粒度的控制,但是我谦卑地建议您那时使用了错误的语言;或至少是错误的运行时(那里有RT Java产品,我认为它们更适合这种情况).

Of course it could well be that your code needs more fine grained control over memory, but I'd humbly suggest you're using the wrong language then; or at least the wrong runtime (there's RT java offerings out there, I assume they're more geared toward this sort of thing).

很抱歉,这似乎不是最有用的答案,但基本上我想知道您是否真的需要这个吗?

I'm sorry if this doesn't seem like the most useful answer, but basically I'm wondering whether you really need this?

这篇关于我如何估计调用System.gc()留下的内存量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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