为什么gc()没有释放内存? [英] Why does gc() not free memory?

查看:287
本文介绍了为什么gc()没有释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用<64 GB RAM 在 Windows 64位计算机上运行模拟。内存使用率达到 55%,并且在完成模拟运行后,我通过 rm(list = ls())删除工作空间中的所有对象,随后由 double gc()



我认为这会为下次模拟运行释放足够的内存,但实际内存使用量仅下降1%。咨询了很多不同的论坛,我无法找到令人满意的解释,只有模糊的评论,如:


根据您的操作系统中,释放的内存可能不会返回到操作系统,而是保留在进程空间中。


我想查找以下信息:


    <1> 1)哪些操作系统和哪些条件释放内存不会返回到操作系统, / li>
  • 2)是否有任何其他补救措施比关闭R并在下次模拟运行时再次启动?


解决方案

你如何检查内存使用情况?通常,虚拟机会分配一些用于存储数据的内存块。部分分配可能未使用并标记为空闲。 GC所做的工作就是发现未在其他地方引用的数据,并将相应的内存块标记为未使用,这并不意味着此内存已发布到操作系统。仍然从虚拟机的角度来看,现在有更多可用于进一步计算的自由内存。

其他人问你是否遇到内存不足的错误?如果没有,那么没有什么可担心的。



编辑:
应该足以理解R中的内存分配和垃圾回收是如何工作的。



从第一个文档:


偶尔尝试将未使用的页面释放回
操作系统。当页面被释放时,许多空闲节点
等于R_MaxKeepFrac乘以每个
类的分配节点数。不需要满足此要求的页面是
发布。尝试释放页面的时间为每个R_PageReleaseFreq级别1
或级别2的集合。


编辑2:



要查看使用过的内存,请尝试将verbose设置为TRUE的gc():

  gc (verbose = T)

下面是一个10'000'000整数数组的结果:

 垃圾收集9 = 1 + 0 + 8(等级2)... 
使用10.7 MB的cons单元(Mb)gb触发(Mb)最大使用(Mb)
Ncells 198838 10.7 407500 21.8 350000 18.7
使用的载体(72%)40.6 Mbytes Vcells 5311050 40.6 7421749 56.7 5311504 40.6

此处放弃引用之后:

 垃圾回收10 = 1 + 0 + 9(等级2)... 
使用10.7 MB的cons单元(49%)
使用的2.4 Mbytes向量(5%)使用的
(Mb)gc触发(Mb)使用的最大值(Mb)
Ncells 198821 10.7 4 07500 21.8 350000 18.7
Vcells 310987 2.4 5937399 45.3 5311504 40.6

正如您所看到的使用内存由Vcells从40.6Mb下降到2.4Mb。

I run simulations on a Windows 64bit-computer with 64 GB RAM. Memory use reaches 55% and after a finished simulation run I remove all objects in the working space by rm(list=ls()), followed by a double gc().

I supposed that this would free enough memory for the next simulation run, but actually memory usage drops by just 1%. Consulting a lot of different fora I could not find a satisfactory explanation, only vague comments such as:

"Depending on your operating system, the freed up memory might not be returned to the operating system, but kept in the process space."

I'd like to find information on:

  • 1) which OS and under which conditions freed memory is not returned to the OS, and
  • 2) if there is any other remedy than closing R and start it again for the next simulation run?

解决方案

How do you check memory usage? Normally virtual machine allocates some chunk of memory that it uses to store its data. Some of the allocated may be unused and marked as free. What GC does is discovering data that is not referenced from anywhere else and marking corresponding chunks of memory as unused, this does not mean that this memory is released to the OS. Still from the VM perspective there's now more free memory that can be used for further computation.

As others asked did you experience out of memory errors? If not then there's nothing to worry about.

EDIT: This and this should be enough to understand how memory allocation and garbage collection works in R.

From the first document:

Occasionally an attempt is made to release unused pages back to the operating system. When pages are released, a number of free nodes equal to R_MaxKeepFrac times the number of allocated nodes for each class is retained. Pages not needed to meet this requirement are released. An attempt to release pages is made every R_PageReleaseFreq level 1 or level 2 collections.

EDIT2:

To see used memory try running gc() with verbose set to TRUE:

gc(verbose=T)

Here's a result with an array of 10'000'000 integers in memory:

Garbage collection 9 = 1+0+8 (level 2) ... 
10.7 Mbytes of cons cells used (49%)
40.6 Mbytes of vectors used (72%)
          used (Mb) gc trigger (Mb) max used (Mb)
Ncells  198838 10.7     407500 21.8   350000 18.7
Vcells 5311050 40.6    7421749 56.7  5311504 40.6

And here's after discarding reference to it:

Garbage collection 10 = 1+0+9 (level 2) ... 
10.7 Mbytes of cons cells used (49%)
2.4 Mbytes of vectors used (5%)
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 198821 10.7     407500 21.8   350000 18.7
Vcells 310987  2.4    5937399 45.3  5311504 40.6

As you can see memory used by Vcells fell from 40.6Mb to 2.4Mb.

这篇关于为什么gc()没有释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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