对 Julia 中的内存分配和垃圾收集感到困惑 [英] Confused by memory allocation and garbage collection in Julia

查看:29
本文介绍了对 Julia 中的内存分配和垃圾收集感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Julia 中的内存分配有点困惑.我从常见问题解答中知道已完成清除大变量使用的内存通过将其设置为较小的值(如 0),然后运行 ​​gc().

I am a bit confused by memory allocation in Julia. I know from the FAQ that clearing the memory used by a large variable is done by setting it to something small (like 0) and then running gc().

但是,我对以下内容有些困惑.我创建了一个随机 Float32 数组:

However, I'm a bit confused by the following. I create a random Float32 array:

@time A = rand(Float32, 10000, 10000);

time 表示分配了大约 400MB 的 RAM,Julia 的 RAM 使用量增加了 400MB.这是有道理的.

time indicates that ~400MB of RAM was allocated, and Julia's RAM usage increases by 400MB. This makes sense.

然后我应用 fft,但不要将结果绑定到任何变量:

I then apply fft, but don't bind the result to any variable:

@time fft(A);

time 表示分配了大约 800MB 的 RAM,而 Julia 的 RAM 使用量增加了 800MB.

time indicates that ~800MB of RAM was allocated, and Julia's RAM usage increases by 800MB.

但是,RAM 使用量仍然比开始时高 1.2GB.这让我很困惑,因为我没有将任何变量等同于 fft(A),所以我希望在执行 fft 后立即释放分配的 800MB.

However, the RAM usage remains at 1.2GB higher than at the start. And that confuses me, because I didn't equate any variable to fft(A), so I would expect that the 800MB allocated would be immediately freed after the fft was executed.

我尝试运行 gc,以为 Julia 会意识到还有 800MB 的 RAM 被无用使用:

I tried to run gc, thinking that Julia would realize that there was an additional 800MB of RAM that was being used for nothing:

gc();

这没有任何作用.RAM 使用率保持在 ~1.3GB.

This does nothing. RAM usage remains at ~1.3GB.

但是,下面两行,

A = 0;
gc();

释放所有正在使用的 1.2GB,尽管 sizeof(A) 只有 400MB.所以我的问题是:

frees all 1.2GB that is in use, despite the fact that sizeof(A) is only 400MB. So my question is:

  • 为什么看起来好像一个大小为 400MB 的对象(根据 sizeof)实际上分配了 1.2GB?
  • Why does it appear as though an object which is 400MB in size (according to sizeof) actually has 1.2GB allocated to it?

推荐答案

每个命令都会返回一些东西,即使它只是 nothing.ans 被分配给每个返回的对象,即使没有直接分配,即使命令以分号结尾.

Each command returns something, even if it is only nothing. ans is assigned to each returned object even if there is no direct assignment and even if the command ends with a semicolon.

** 编辑 ** [Julia 版本≥ v0.7.0 的更新信息]

** EDIT ** [Updated info for Julia version ≥ v0.7.0]

在命令之间使用 varinfo() 用于 Julia v0.7.0 及更高版本(whos() 用于 Julia v0.6.4 及更低版本)以查看分配和分配的空间.

Use varinfo() for Julia v0.7.0 and higher (whos() for Julia v0.6.4 and lower) between commands to watch assignments and the allocated space.

这篇关于对 Julia 中的内存分配和垃圾收集感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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