R 中的堆分析(需要解决将许多绘图写入 png/jpeg 的问题)

发布于 2024-10-15 22:24:10 字数 730 浏览 1 评论 0原文

我在生成许多绘图并将其写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2024-10-22 22:24:10

听起来确实好像内存不足并交换到磁盘。

您可以使用 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 optional verbose parameter. Try adding it after dev.off() and see if it helps.

Jeffrey

分开我的手 2024-10-22 22:24:10

好吧,看起来这是我对 qplot 的调用(错误或飞行员错误)的问题,而不是我最初想到的对 png/jpeg/eps 的渲染问题。

要重现,首先将底部列出的表复制到名为“bad.data”的文件中,然后在 R 提示符中键入以下内容:

  require(ggplot2)
  df_import <- read.table("bad.data", colClasses=c("integer", "POSIXct","factor","integer"), header = TRUE)
  qplot(EFF_DATE, value, data = df_import)



        EFF_DATE variable value
147170 2010-07-05  COUNT_0     0
147254 2010-07-06  COUNT_0     0
147338 2010-07-07  COUNT_0     0
147422 2010-07-08  COUNT_0     0
147506 2010-07-09  COUNT_0     0
147590 2010-07-12  COUNT_0     0
147674 2010-07-13  COUNT_0     0
147758 2010-07-15  COUNT_0     0
147842 2010-07-16  COUNT_0     0
147926 2010-07-19  COUNT_0     0
148010 2010-07-20  COUNT_0     0
148094 2010-07-21  COUNT_0     0
148178 2010-07-22  COUNT_0     0
148262 2010-07-23  COUNT_0     0
148346 2010-07-26  COUNT_0     0
148430 2010-07-28  COUNT_0     0
148514 2010-07-29  COUNT_0     0
148598 2010-07-30  COUNT_0     0
148682 2010-08-02  COUNT_0     0
148766 2010-08-03  COUNT_0     0
148850 2010-08-04  COUNT_0     0
148934 2010-08-05  COUNT_0     0
149018 2010-08-06  COUNT_0     0
149102 2010-08-09  COUNT_0     0
149186 2010-08-10  COUNT_0     0
149271 2010-08-11  COUNT_0     0
149356 2010-08-12  COUNT_0     0
149439 2010-08-13  COUNT_0     0
149521 2010-08-16  COUNT_0     0
149601 2010-08-17  COUNT_0     0
149681 2010-08-18  COUNT_0     0
149761 2010-08-19  COUNT_0     0
149843 2010-08-20  COUNT_0     0
149925 2010-08-23  COUNT_0     0
150004 2010-08-24  COUNT_0     0
150084 2010-08-25  COUNT_0     0
150164 2010-08-26  COUNT_0     0
150245 2010-08-27  COUNT_0     0
150326 2010-08-30  COUNT_0     0
150407 2010-08-31  COUNT_0     0

我的体系结构是 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:

  require(ggplot2)
  df_import <- read.table("bad.data", colClasses=c("integer", "POSIXct","factor","integer"), header = TRUE)
  qplot(EFF_DATE, value, data = df_import)



        EFF_DATE variable value
147170 2010-07-05  COUNT_0     0
147254 2010-07-06  COUNT_0     0
147338 2010-07-07  COUNT_0     0
147422 2010-07-08  COUNT_0     0
147506 2010-07-09  COUNT_0     0
147590 2010-07-12  COUNT_0     0
147674 2010-07-13  COUNT_0     0
147758 2010-07-15  COUNT_0     0
147842 2010-07-16  COUNT_0     0
147926 2010-07-19  COUNT_0     0
148010 2010-07-20  COUNT_0     0
148094 2010-07-21  COUNT_0     0
148178 2010-07-22  COUNT_0     0
148262 2010-07-23  COUNT_0     0
148346 2010-07-26  COUNT_0     0
148430 2010-07-28  COUNT_0     0
148514 2010-07-29  COUNT_0     0
148598 2010-07-30  COUNT_0     0
148682 2010-08-02  COUNT_0     0
148766 2010-08-03  COUNT_0     0
148850 2010-08-04  COUNT_0     0
148934 2010-08-05  COUNT_0     0
149018 2010-08-06  COUNT_0     0
149102 2010-08-09  COUNT_0     0
149186 2010-08-10  COUNT_0     0
149271 2010-08-11  COUNT_0     0
149356 2010-08-12  COUNT_0     0
149439 2010-08-13  COUNT_0     0
149521 2010-08-16  COUNT_0     0
149601 2010-08-17  COUNT_0     0
149681 2010-08-18  COUNT_0     0
149761 2010-08-19  COUNT_0     0
149843 2010-08-20  COUNT_0     0
149925 2010-08-23  COUNT_0     0
150004 2010-08-24  COUNT_0     0
150084 2010-08-25  COUNT_0     0
150164 2010-08-26  COUNT_0     0
150245 2010-08-27  COUNT_0     0
150326 2010-08-30  COUNT_0     0
150407 2010-08-31  COUNT_0     0

My architecture is Linux x86_64, although I'm not sure if this is relevant to the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文