如何识别ETS表的精确内存大小? [英] How to identify the exact memory size of an ETS table?

查看:590
本文介绍了如何识别ETS表的精确内存大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据给ETS表,info / 1函数返回表的各种属性,包括特定于行数而不是物理大小的大小值。



有没有办法计算ETS表占用的内存量?

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


解决方案

TL; DR:



分配内存大小(以字节为单位)的ETS表:



ets:info(表,记忆)* erlang:system_info(wordsize)。






详细说明一下, ets:info(表,内存)给你分配给ETS表中数据的字
(与Mnesia相同)您可以在 TV 应用程序。DETS表的相同属性以字节为单位。



一个字只不过是自然数据特定CPU架构的单位。代表什么取决于您的架构:32位或64位(或使用 erlang:system_info(wordsize)立即获取正确的字大小)




  • 32位系统中,一个字是4个字节(32位)。

  • 64位系统中,一个字是8个字节(64位)。



另外请注意,ETS表格最初跨越768个字词,您必须添加大小的每个元素,6个字+大小的Erlang数据。不清楚这些是分配给数据的单词 ets:info 指定。



大小有点麻烦:ETS表有自己独立的内存管理系统,经过优化和垃圾回收,可根据表格类型(设置 bag duplicate_bag )。作为一个实验,空表在我的环境中返回分配给数据的300个字。如果我添加11元组,大小增加到366字。没有想法如何与初始的768个词相关,或者为什么大小只增加11 * 6个字,当它应该是11 * 6 + 11 * 1(11个原子),根据定义。

$然而,采用初始表大小和分配给数据的单词(例如22086个字)的天真估计结果为768 * 8 + 22.086 * 8 = 182.832字节(178.54 KiB)。



当然,数据越大,那些结构字就越重要,所以你只能使用分配给数据的字数字,由 ets:info ,估计你的表的大小记忆。






编辑:还有另外两个功能可以让您审核ETS内存使用情况:




  • erlang :memory / 1 erlang:memory(ets)返回分配给ETS的内存大小(以字节为单位)。

  • ets:i / 0 :所有活动ETS表的概述(有点像在 TV中查看系统表,但使用类型和内存数据)。



作为一个小测试,新创建的空表增加了内存使用312个字(2.44 KiB),远远少于手册中的768个数字(也许是CPU架构相关的,我不知道),而ETS本身报告了299个字(2.33 KiB)分配给数据。



只有13个字(104字节)的结构性开销(或似乎,它似乎保持模糊)从增加 erlang:memory / 1 报告,所以 ets:info / 2 毕竟是相当准确的。



在插入一个由2个原子组成的简单元组后, erlang:memory / 1 报告了一个8字内存分配增加,就像文档说的那样(新的ETS记录: 6个字+数据大小 - 在这种情况下为2个:每个原子1个字。)


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.

Is there any way to calculate the amount of memory in bytes occupied by an ETS table ?

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

解决方案

TL;DR:

ETS table allocated memory size in bytes:

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).

A word is nothing more than the 'natural' data unit of a particular CPU architecture. 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)

  • On a 32-bit system, a word is 4 bytes (32 bits).
  • On a 64-bit system, a word is 8 bytes (64 bits).

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. It's not really clear if those are the words "allocated to data" ets:info specifies.

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). As an experiment, an empty table returns, in my environment, 300 words "allocated to data". If I add 11 tuples, size increases to 366 words. 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.

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).

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.


Edit: There are two other functions that let you audit ETS memory usage:

  • erlang:memory/1: erlang:memory(ets) returns the memory size, in bytes, allocated to 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).

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.

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.

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).

这篇关于如何识别ETS表的精确内存大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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