简体   繁体   English

如何识别ETS表的确切内存大小?

[英]How to identify the exact memory size of an ETS table?

Give an ETS table with data, the info/1 function returns various properties for the table, including a size value which is specific to the number of rows rather than the physical size. 给具有数据的ETS表中,方式/ 1函数返回表中的各种属性,包括一个尺寸值是特定于行数,而不是实际尺寸。

Is there any way to calculate the amount of memory in bytes occupied by an ETS table ? 有没有办法计算ETS表占用的内存量?

ets:new( mytable, [bag, named_table, compressed]),
ets:insert( mytable, { Key, Value } ),
....
ets:info ( mytable ).

TL;DR: TL; DR:

ETS table allocated memory size in bytes: ETS表分配的内存大小以字节为单位:

ets:info(Table,memory) * erlang:system_info(wordsize).


To elaborate a bit, ets:info(Table,memory) gives you the words allocated to data in an ETS table (same for Mnesia. You can view al that info in the TV application. The same attribute for DETS tables is in bytes). 为了详细说明, ets:info(Table,memory)为您提供分配给ETS表中数据的单词(对于Mnesia也是如此。您可以在TV应用程序中查看该信息.DETS表的相同属性以字节为单位) 。

A word is nothing more than the 'natural' data unit of a particular CPU architecture. 一个词只不过是特定CPU架构的“自然”数据单元。 What that represents depends on your architecture: 32-bit or 64-bit (or use erlang:system_info(wordsize) to get the correct word size immediately) 代表什么取决于您的架构:32位或64位(或使用erlang:system_info(wordsize)来立即获得正确的字大小)

  • On a 32-bit system, a word is 4 bytes (32 bits). 32位系统上,一个字是4个字节(32位)。
  • On a 64-bit system, a word is 8 bytes (64 bits). 64位系统上,一个字是8个字节(64位)。

Also note that a ETS table initially spans 768 words , to wich you must add the size of each element, 6 words + size of Erlang data. 另请注意,ETS表最初跨越768个单词 ,您必须添加每个元素的大小,6个单词+大小的Erlang数据。 It's not really clear if those are the words "allocated to data" ets:info specifies. 目前还不清楚这些是“分配给数据” ets:info指定。

Calculating the exact size is a bit of a hassle: ETS tables have their own independent memory management system, which is optimized and garbage collected, and can vary depending on table type ( set , bag , duplicate_bag ). 计算确切的大小有点麻烦:ETS表有自己独立的内存管理系统,它经过优化和垃圾收集,并且可以根据表类型( setbagduplicate_bag )而变化。 As an experiment, an empty table returns, in my environment, 300 words "allocated to data". 作为一个实验,一个空表在我的环境中返回300个“分配给数据”的单词。 If I add 11 tuples, size increases to 366 words. 如果我添加11个元组,则大小增加到366个单词。 No Idea to how those relate to the initial 768 words, or why the size only increases by 11*6 words, when it should have been 11*6 + 11*1 (11 atoms), according to definition. 根据定义,不知道那些与最初的768个单词有什么关系,或者为什么大小只会增加11 * 6个单词,而它应该是11 * 6 + 11 * 1(11个原子)。

Still, a naive estimate, taking the initial table size and the words allocated to data, for example 22086 words, results in 768*8 + 22.086*8 = 182.832 bytes (178.54 KiB). 仍然,一个简单的估计,取初始表大小和分配给数据的单词,例如22086字,导致768 * 8 + 22.086 * 8 = 182.832字节(178.54 KiB)。

Of course, the bigger the data, the less those "structural" words matter, so you could only use the "words allocated to data" number, returned by ets:info , to estimate your table's size in memory. 当然,数据越大,“结构”单词就越少,所以你只能使用ets:info返回的“分配给数据的单词”数来估计你的表在内存中的大小。


Edit : There are two other functions that let you audit ETS memory usage: 编辑 :还有两个其他功能可以让您审核ETS内存使用情况:

  • erlang:memory/1 : erlang:memory(ets) returns the memory size, in bytes, allocated to ETS. erlang:memory/1erlang:memory(ets)返回分配给ETS的内存大小(以字节为单位)。
  • ets:i/0 : an overview of all active ETS tables (a bit like viewing system tables in TV , but with type and memory data). ets:i/0 :所有活动ETS表的概述(有点像在TV查看系统表,但有类型和内存数据)。

As a small test, an newly created empty table increased memory use with 312 words (2.44 KiB), a lot less than the 768 number in the manual (perhaps it's CPU architecture related, I have no idea), while ETS itself reported 299 words (2.33 KiB) allocated to the data. 作为一个小测试,一个新创建的空表增加了内存使用312字(2.44 KiB),比手册中的768号少很多(也许它的CPU架构相关,我不知道),而ETS本身报告了299个字(2.33 KiB)分配给数据。

That's only 13 words (104 bytes) of structural overhead away (or so it seems, it remains nebulous) from the increase erlang:memory/1 reported, so ets:info/2 is fairly accurate after all. 这只是13字(104字节)的结构开销(或者似乎,它似乎,它仍然模糊)来自增加的erlang:memory/1报告,所以ets:info/2毕竟是相当准确的。

After the insertion of a simple tuple consisting of 2 atoms, erlang:memory/1 reported a 8 word memory allocation increase, just like the documentation said it would (new ETS record: 6 words + size of data - 2 in this case : 1 word per atom). 在插入一个由2个原子组成的简单元组之后, erlang:memory/1报告了8字内存分配增加,就像文档所说的那样(新的ETS记录:6个字+数据大小 - 在这种情况下为2:1每个原子的单词)。

you can read the document about ets . 你可以阅读有关ets的文件。

you can ues this to get the memory allocated to the table. 你可以使用它来获取分配给表的内存。 ets:info ( mytable, memory). ets:info(mytable,memory)。

{memory, integer() >= 0 The number of words allocated to the table. {memory,integer()> = 0分配给表的字数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM