R 内存使用量一直在增长,直到崩溃
我正在从R运行一些脚本,从一些网站获取信息。问题在于,即使我使用R 内存使用量一直在增长,直到崩溃,r,memory,memory-management,R,Memory,Memory Management,我正在从R运行一些脚本,从一些网站获取信息。问题在于,即使我使用gc()清理会话,内存仍在不断增长,直到会话崩溃 以下是脚本: library(XML) library(RJDBC) library(RCurl) procesarPublicaciones <- function(tabla){ log_file <<- file(log_path, open="a") drv <<- JDBC("oracle.jdbc
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)
}
--------------编辑2015-06-08------------------
我仍然有这个问题,但我在其他帖子上发现了同样的问题,显然已经解决了
使用XML包时,您需要使用
free()
释放htmlpasse()
分配的内存(或在C级别分配内存的任何其他html解析函数)。当我不再需要html文档时,我通常会立即调用free(doc)
因此,在您的情况下,我会尝试将free(doc)
放在您函数中rm(info1,info2,data,doc)
之前的行中,如下所示:
free(doc)
rm(info1, info2, data, doc)
事实上,调用
free()
可能足以完全删除rm()
调用。我在使用HTMLPase时遇到了一个相关问题。导致在我的10000次迭代完成之前Windows崩溃(内存不足)
答复:
除了free/remove-每隔n次迭代执行一次垃圾收集gc()(如中所建议的)我在rm()
之前添加free()
,但不起作用。即使比以前慢,记忆仍在增长。还有别的建议吗?
free(doc)
rm(info1, info2, data, doc)