【问题标题】:Memory usage keep growing until crash内存使用量持续增长直到崩溃
【发布时间】:2015-05-22 15:13:14
【问题描述】:

我正在从 R 运行一些脚本,这些脚本从一些网站获取信息。问题是即使我使用gc() 清理会话,内存仍会不断增长,直到我的会话崩溃。

这是脚本:

library(XML)
library(RJDBC)
library(RCurl)

    procesarPublicaciones <- function(tabla){

        log_file <<- file(log_path, open="a")

        drv <<- JDBC("oracle.jdbc.OracleDriver", classPath="C:/jdbc/jre6/ojdbc6.jar"," ")
        con <<- dbConnect(drv, "server_path", "user", "password")

        query <- paste("SELECT * FROM",tabla,sep=' ')

        bool <- tryCatch( 
                { 
                ## Get a list of URLs from a DB
                listUrl <- dbGetQuery(con, query)
                if( nrow(listUrl) != 0) TRUE else FALSE
                dbDisconnect(con)
                },  error = function(e) return(FALSE)
                )
        if( bool ) {

            file.create(data_file)
            apply(listUrl,c(1),procesarHtml)
        }else{
            cat("\n",getTime(),"\t[ERROR]\t\t", file=log_file)
        }
        cat( "\n",getTime(),"\t[INFO]\t\t FINISH", file=log_file)
        close(log_file)
    }

    procesarHtml <- function(pUrl){

        headerGatherer <- basicHeaderGatherer()
        html <- getURI(theUrl, headerfunction = headerGatherer$update, curl = curlHandle)
        heatherValue <- headerGatherer$value()

        if ( heatherValue["status"] == "200" ){

            doc <- htmlParse(html)
            tryCatch
            (
                {
                    ## Here I get all the info that I need from the web and write it on a file.
                    ## here is a simplification
                    info1 <- xpathSApply(doc, xPath.info1, xmlValue)
                    info2 <- xpathSApply(doc, xPath.info2, xmlValue)
                    data <- data.frame(col1 = info1, col2=info2)
                    write.table(data, file=data_file , sep=";", row.names=FALSE, col.names=FALSE, append=TRUE)
                }, error= function(e)
                {
                    ## LOG ERROR
                }
            )
            rm(info1, info2, data, doc)
        }else{
            ## LOG INFO
        }
        rm(headerGatherer,html,heatherValue)
            cat("\n",getTime(),"\t[INFO]\t\t memory used: ", memory.size()," MB", file=log_file)
            gc()
            cat("\n",getTime(),"\t[INFO]\t\t memory used after gc(): ", memory.size()," MB", file=log_file)
    }

即使我使用rm() 删除所有内部变量并使用gc(),内存仍在增长。我从网上得到的所有 html 似乎都保存在内存中。

这是我的会话信息:

> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows XP (build 2600) Service Pack 3

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RCurl_1.95-4.6 bitops_1.0-6   RJDBC_0.2-5    rJava_0.9-6    DBI_0.3.1     
[6] XML_3.98-1.1  

loaded via a namespace (and not attached):
[1] tools_3.2.0

--------编辑 2015-06-08 ------------------ --

我还是有这个问题,但是我在其他帖子上发现了同样的问题,显然已经解决了。

Serious Memory Leak When Iteratively Parsing XML Files

【问题讨论】:

    标签: r memory memory-management


    【解决方案1】:

    使用 XML 包时,您需要使用 free() 来释放由 htmlParse()(或任何其他在 C 级别分配内存的 html 解析函数)分配的内存。当我不再需要 html 文档时,我通常会立即致电 free(doc)

    因此,在您的情况下,我会尝试在您的函数中将 free(doc) 放在自己的行上,然后放在 rm(info1, info2, data, doc) 之前,如下所示:

    free(doc)
    rm(info1, info2, data, doc)
    

    事实上,对free() 的调用可能就足够了,您可以完全删除rm() 调用。

    【讨论】:

    • 我在rm() 之前添加了free(),但不起作用。即使比以前慢,内存也会不断增长。还有其他建议吗?
    【解决方案2】:

    我在使用 htmlParse 时遇到了相关问题。在我完成 10,000 次迭代之前导致 Windows 崩溃(内存不足)。

    回答: 除了 free/remove - 每 n 次迭代执行一次垃圾收集 gc()(如 Serious Memory Leak When Iteratively Parsing XML Files 中所建议的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多