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

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

问题描述

我在具有 64 GB RAMWindows 64 位计算机 上运行模拟.内存使用达到 55%,在完成模拟运行后,我通过 rm(list=ls()) 删除工作空间中的所有对象,然后是 doublegc().

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().

我以为这会为下一次模拟运行释放足够的内存,但实际上内存使用仅下降了 1%.咨询了很多不同的论坛,我找不到满意的解释,只有模糊的评论,例如:

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:

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

我想查找以下信息:

  • 1) 哪个操作系统以及在何种条件下释放了内存不会返回给操作系统,以及
  • 2) 除了关闭 R 并重新启动它以进行下一次模拟运行之外,是否还有其他补救措施?

推荐答案

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

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.

这个this 应该足以理解 R 中的内存分配和垃圾回收是如何工作的.

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

从第一个文档开始:

有时会尝试将未使用的页面释放回操作系统.当页面被释放时,一些空闲节点等于 R_MaxKeepFrac 乘以每个分配的节点数类被保留.不需要满足此要求的页面是释放.每个 R_PageReleaseFreq 级别 1 都会尝试释放页面或 2 级集合.

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.

要查看已用内存,请尝试运行 gc(),并将详细设置为 TRUE:

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

gc(verbose=T)

这是内存中包含 10'000'000 个整数的数组的结果:

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

如您所见,Vcell 使用的内存从 40.6Mb 下降到 2.4Mb.

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

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

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