我如何获得一个Ruby对象在Rails中MB的大小? [英] How do I get the size of a ruby object in mb in Rails?

查看:293
本文介绍了我如何获得一个Ruby对象在Rails中MB的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要查询一个ActiveRecord模型,修改它,并计算以MB为单位的新对象的大小。如何做到这一点?

I want to query an ActiveRecord model, modify it, and calculate the size of the new object in mb. How do I do this?

推荐答案

数据行中的数据库的大小以及红宝石对象在内存中的对象尺寸都不易得到不幸

The size of data rows in a database as well as the object size of ruby objects in memory are both not readily available unfortunately.

虽然这是一个有点更容易获得在内存中的对象大小的感觉,你仍然必须找到这把你的活动记录对象的一部分,所有的对象,因此应该算(这是不是很明显)。即使是这样,你就必须处理诸如共享/缓存数据,类开销,这可能需要数无明显的事情,但不是必须的。

While it is a bit easier to get a feeling for the object size in memory, you would still have to find all objects which take part of your active record object and thus should be counted (which is not obvious). Even then, you would have to deal with non-obvious things like shared/cached data, class overhead, which might be required to count, but doesn't have to.

在数据库端,它在很大程度上取决于所使用的存储引擎。从你的数据库的文档,就可以正常扣除的存储要求每一个你在表中定义的列(这可能会改变的情况下,VARCHAR,TEXT或BLOB列的,在这上面来共享资源,如索引,一般表中的开销,......为了得到一个估计值,为表中各列的文件大小要求应该是足够尽管

On the database side, it heavily depends on the storage engine used. From the documentation of your database, you can normally deduct the storage requirements for each of the columns you defined in your table (which might vary in case of VARCHAR, TEXT, or BLOB columns. On top of this come shared resources like indexes, general table overhead, ... To get an estimate, the documented size requirements for the various columns in your table should be sufficient though

通常情况下,这是真的很难得到一个正确的大小如数据库行或内存中的对象复杂的事情。该系统没有建立收​​集或提供这些信息。

Generally, it is really hard to get a correct size for complex things like database rows or in-memory objects. The systems are not build to collect or provide this information.

除非你绝对肯定需要得到一个确切的数据,那么你最好的太多空间的一侧。通常,数据库它不会伤害有太多的磁盘空间(在这种情况下,数据库通常会运行得更快一点)或过多的内存(这将减少内存pressure对Ruby这将再次让它更快)。

Unless you absolutely positively need to get an exact data, you should err on the side of too much space. Generally, for databases it doesn't hurt to have too much disk space (in which case, the database will generally run a little faster) or too much memory (which will reduce memory pressure for Ruby which will again make it faster).

通常情况下,红宝石进程的内存使用量将不明显。因此,最好的行动过程中几乎总是写你的程序,然后将其与实际数据所需量的测试和检查它的性能和内存的要求。这样,你得到的实际的信息需要,即:有多少内存我的程序需要处理我需要的数据集时

Often, the memory usage of Ruby processes will be not obvious. Thus, the best course of action is almost always to wrote your program and then test it with the desired amount of real data and check its performance and memory requirements. That way, you get the actual information you need, namely: how much memory does my program need when handling my required dataset.

这篇关于我如何获得一个Ruby对象在Rails中MB的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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