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

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

问题描述

Julia的内存分配让我感到困惑。我通过常见问题解答了解清除大变量使用的内存已完成通过设置它为小(如0),然后运行 gc()



然而,我有点困惑以下。我创建了一个随机的 Float32 数组:

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

时间表示〜400MB的RAM是分配,并且Julia的RAM使用量增加了400MB。这是有道理的。然后我应用 fft ,但不要将结果绑定到任何变量:

$ b
$ b

  @time fft(A); 

时间表示〜800MB的内存为分配,并且Julia的内存使用量增加了800MB。



然而,内存使用量仍然比开始时高1.2GB。这让我感到困惑,因为我没有将任何变量等同于 fft(A),所以我预计分配的800MB将在<$ c $后立即释放c> fft 被执行。



我试图运行 gc 会意识到还有一个额外的800MB的RAM没有被使用:

  gc(); 

这没什么用。 RAM使用率保持在〜1.3GB。



然而,以下两行,

  A = 0; 
gc();

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


  • 为什么它看起来像一个400MB大小的对象(根据 sizeof )实际上有1.2GB的分配给它吗?


解决方案

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



在命令之间使用 whos()来查看赋值和分配的空间。


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

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

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

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

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

@time fft(A);

time indicates that ~800MB of RAM was allocated, and Julia's RAM usage increases by 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.

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

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

However, the following two lines,

A = 0;
gc();

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

  • Why does it appear as though an object which is 400MB in size (according to sizeof) actually has 1.2GB allocated to it?

解决方案

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.

Use whos() between commands to watch assignments and the allocated space.

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

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