为什么在加载缓存的对象时不会显着增加它们的内存消耗? [英] Why does loading cached objects increase the memory consumption drastically when computing them will not?

查看:72
本文介绍了为什么在加载缓存的对象时不会显着增加它们的内存消耗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个可以通过配置文件进行自定义的小软件.解析配置文件并将其转换为嵌套的环境结构(例如.HIVE $ db =环境,.HIVE $ db $ user =最坏",.HIVE $ db $ pw =我的密码",.HIVE $ regex $日期=一些用于日期等的正则表达式)

I've built a little software that can be customized via a config file. The config file is parsed and translated into a nested environment structure (e.g. .HIVE$db = an environment, .HIVE$db$user = "Horst", .HIVE$db$pw = "my password", .HIVE$regex$date = some regex for dates etc.)

我建立了可以处理这些嵌套环境的例程(例如,查找值"db/user"或"regex/date",对其进行更改等).事实是,配置文件的初始解析需要很长时间,并且会导致很大的一个对象(实际上是3到4,介于4到16 MB之间).因此,我认为没问题,让我们通过将对象保存到.Rdata文件来缓存它们".这可行,但是加载"缓存的对象使我的Rterm进程在RAM消耗(超过1 GB!)方面费尽周折,而我仍然不太明白为什么(这个没有当我重新计算"对象时发生这种情况,但这正是我要避免的事情,因为它花费的时间太长.

I've built routines that can handle those nested environments (e.g. look up value "db/user" or "regex/date", change it etc.). The thing is that the initial parsing of the config files takes a long time and results in quite a big of an object (actually three to four, between 4 and 16 MB). So I thought "No problem, let's just cache them by saving the object(s) to .Rdata files". This works, but "loading" cached objects makes my Rterm process go through the roof with respect to RAM consumption (over 1 GB!!) and I still don't really understand why (this doesn't happen when I "compute" the object all anew, but that's exactly what I'm trying to avoid since it takes too long).

我已经考虑过可能对其进行序列化,但是我还没有对其进行测试,因为我需要对代码进行一些重构.另外,我不确定是否会以与加载.Rdata文件相同的方式影响重新加载到R"部分.

I already thought about maybe serializing it, but I haven't tested it as I would need to refactor my code a bit. Plus I'm not sure if it would affect the "loading back into R" part in just the same way as loading .Rdata files.

谁能告诉我为什么加载先前计算的对象会对我的Rterm进程的内存消耗产生影响(与在我启动的每个新进程中进行计算相比),以及如何最好地避免这种情况?

Can anyone tell me why loading a previously computed object has such effects on memory consumption of my Rterm process (compared to computing it in every new process I start) and how best to avoid this?

如果需要的话,我也会尝试举一个例子,但是要重现我的确切情况有些棘手.但是我会尽力的.

If desired, I will also try to come up with an example, but it's a bit tricky to reproduce my exact scenario. Yet I'll try.

推荐答案

这很可能是因为您所创建的环境围绕着他们的祖先.如果您不需要祖先信息,则将此类环境的父级设置为emptyenv()(或者如果不需要,则不要使用环境).

Its likely because the environments you are creating are carrying around their ancestors. If you don't need the ancestor information then set the parents of such environments to emptyenv() (or just don't use environments if you don't need them).

还请注意,公式(当然还有函数)具有环境,因此也请注意那些环境.

Also note that formulas (and, of course, functions) have environments so watch out for those too.

这篇关于为什么在加载缓存的对象时不会显着增加它们的内存消耗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆