R 中的堆分析(需要解决将许多绘图写入 png/jpeg 的问题)
我在生成许多绘图并将其写入 png/jpeg/eps 设备时遇到内存问题。
require(ggplot2)
...
render <- function(x) {
fileName=paste(chartDir, "/", x$PACKED[1], ".png", sep="")
x <- x[,c("EFF_DATE", "variable", "value")]
png(fileName, width=1920, height=1000, units="px")
print(qplot(EFF_DATE, value, data = x, facets = variable ~ ., geom="line"))
dev.off()
}
d_ply(molten, "PACKED", render, .progress="tk")
代码在前 80 个图上进展顺利,然后就像一个 fork 炸弹一样,在很短的时间内消耗了 100% 的 RAM。我检查了提供给 qplot 的 x 的大小,它们都大致相同,所以这不是数据。 当我注释 png 行时,代码运行良好。当我尝试使用 ggplot2 库中的 ggsave 时,我遇到了同样的问题。
如果有人知道为什么会发生这种情况,那么我很想听听。然而,预计没有人这样做,有人可以告诉我是否有一个很好的堆分析工具,我可以在 R 中运行它来调查内存的去向,以及是否可以做任何事情来即时清理?我真的不想求助于调试二进制文件。
最好的祝愿, 格雷厄姆.
I'm having memory issues when generating many plots and writing them to png/jpeg/eps devices.
require(ggplot2)
...
render <- function(x) {
fileName=paste(chartDir, "/", x$PACKED[1], ".png", sep="")
x <- x[,c("EFF_DATE", "variable", "value")]
png(fileName, width=1920, height=1000, units="px")
print(qplot(EFF_DATE, value, data = x, facets = variable ~ ., geom="line"))
dev.off()
}
d_ply(molten, "PACKED", render, .progress="tk")
The code progresses nicely for the first ~80 plots and then behaves like a fork bomb thereafter, consuming 100% of RAM within a very short time. I've checked the sizes of x supplied to qplot and they're all roughly the same, so it's not the data.
The code runs fine when I comment the png line. I get the same issue when I try to use ggsave from the ggplot2 library.
If anyone has an inkling as to why this is happening then I'd love to hear it. However, in anticipation that nobody does, can someone tell me if there is a nice heap analysis tool that I can run inside R to investigate where the memory is going and if there's anything I can do to clean up on the fly? I'd really rather not have to resort to debugging the binary.
Best wishes,
Graham.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来确实好像内存不足并交换到磁盘。
您可以使用
gc()
函数强制进行垃圾回收,该函数采用可选的verbose
参数。尝试在dev.off()
之后添加它,看看是否有帮助。杰弗里
It definitely sounds as though you're running out of memory and swapping to disk.
You can force garbage collection with the
gc()
function, which takes an optionalverbose
parameter. Try adding it afterdev.off()
and see if it helps.Jeffrey
好吧,看起来这是我对 qplot 的调用(错误或飞行员错误)的问题,而不是我最初想到的对 png/jpeg/eps 的渲染问题。
要重现,首先将底部列出的表复制到名为“bad.data”的文件中,然后在 R 提示符中键入以下内容:
我的体系结构是 Linux x86_64,尽管我不确定这是否与问题相关。
Ok it looks as though this is a problem with my call to qplot (bug or pilot error) and not the rendering to png/jpeg/eps as I first thought.
To reproduce, first copy the table listed at the bottom into a file called "bad.data" and then type the following into the R prompt:
My architecture is Linux x86_64, although I'm not sure if this is relevant to the problem.