【发布时间】:2014-12-05 07:29:11
【问题描述】:
我已经为我之前的问题 (Julia allocates huge amount of memory for unknown reason) 创建了一个最小工作示例,以隔离问题。这可以直接在 REPL 中测试。考虑代码:
function test1(n)
s = zero(Float64)
for i = 1:10^n
s += sqrt(rand()^2 + rand()^2 + rand()^2)
end
return s
end
--
function test2(n)
@parallel (+) for i = 1:10^n
sqrt(rand()^2 + rand()^2 +rand()^2)
end
end
--
function test3(n)
function add(one, two, three)
one + two + three
end
@parallel (+) for i = 1:10^n
sqrt(add(rand()^2, rand()^2, rand()^2))
end
end
然后,我测试代码:
@time test1(8);
@time test1(8);
@time test2(8);
@time test2(8);
@time test3(8);
@time test3(8);
这是输出:
elapsed time: 1.017241708 seconds (183868 bytes allocated)
elapsed time: 1.033503964 seconds (96 bytes allocated)
elapsed time: 1.214897591 seconds (3682220 bytes allocated)
elapsed time: 1.020521156 seconds (2104 bytes allocated)
elapsed time: 15.23876415 seconds (9600679268 bytes allocated, 26.69% gc time)
elapsed time: 15.418865707 seconds (9600002736 bytes allocated, 26.19% gc time)
谁能解释一下:
- 为什么每个函数的第一次运行都会分配这么多内存?
- 为什么
test2(8)分配的内存比test1(8)高?他们做同样的事情。 - 最重要的是,
test3(8)到底是怎么回事?它分配了大量内存。
编辑:
Julia Version 0.3.1
Commit c03f413* (2014-09-21 21:30 UTC)
Platform Info:
System: Darwin (x86_64-apple-darwin13.3.0)
CPU: Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
LAPACK: libopenblas
LIBM: libopenlibm
LLVM: libLLVM-3.3
【问题讨论】:
-
这不会发生在我身上。
versioninfo()说什么? -
@rickhg12hs 编辑添加 Julia 版本
标签: debugging memory memory-management julia