Limiting Memory Usageedit
On this page
- Elasticsearch - The Definitive Guide: 2.x (current) 1.x
- Foreword
- Preface
- Getting Started
- Search in Depth
- Dealing with Human Language
- Aggregations
- Geolocation
- Modeling Your Data
- Administration, Monitoring, and Deployment
In order for aggregations (or any operation that requires access to field values) to be fast, access to fielddata must be fast, which is why it is loaded into memory. But loading too much data into memory will cause slow garbage collections as the JVM tries to find extra space in the heap, or possibly even an OutOfMemory exception.
It may surprise you to find that Elasticsearch does not load into fielddata just the values for the documents that match your query. It loads the values for all documents in your index, even documents with a different_type
!
The logic is: if you need access to documents X, Y, and Z for this query, you will probably need access to other documents in the next query. It is cheaper to load all values once, and to keep them in memory, than to have to scan the inverted index on every request.
The JVM heap is a limited resource that should be used wisely. A number of mechanisms exist to limit the impact of fielddata on heap usage. These limits are important because abuse of the heap will cause node instability (thanks to slow garbage collections) or even node death (with an OutOfMemory exception).
Fielddata Sizeedit
The indices.fielddata.cache.size
controls how much heap space is allocated to fielddata. When you run a query that requires access to new field values, it will load the values into memory and then try to add them to fielddata. If the resulting fielddata size would exceed the specified size
, other values would be evicted in order to make space.
By default, this setting is unbounded—Elasticsearch will never evict data from fielddata.
This default was chosen deliberately: fielddata is not a transient cache. It is an in-memory data structure that must be accessible for fast execution, and it is expensive to build. If you have to reload data for every request, performance is going to be awful.
A bounded size forces the data structure to evict data. We will look at when to set this value, but first a warning:
This setting is a safeguard, not a solution for insufficient memory.
If you don’t have enough memory to keep your fielddata resident in memory, Elasticsearch will constantly have to reload data from disk, and evict other data to make space. Evictions cause heavy disk I/O and generate a large amount of garbage in memory, which must be garbage collected later on.
Imagine that you are indexing logs, using a new index every day. Normally you are interested in data from only the last day or two. Although you keep older indices around, you seldom need to query them. However, with the default settings, the fielddata from the old indices is never evicted! fielddata will just keep on growing until you trip the fielddata circuit breaker (see Circuit Breaker), which will prevent you from loading any more fielddata.
At that point, you’re stuck. While you can still run queries that access fielddata from the old indices, you can’t load any new values. Instead, we should evict old values to make space for the new values.
To prevent this scenario, place an upper limit on the fielddata by adding this setting to theconfig/elasticsearch.yml
file:
With this setting in place, the least recently used fielddata will be evicted to make space for newly loaded data.
There is another setting that you may see online: indices.fielddata.cache.expire
.
We beg that you never use this setting! It will likely be deprecated in the future.
This setting tells Elasticsearch to evict values from fielddata if they are older than expire
, whether the values are being used or not.
This is terrible for performance. Evictions are costly, and this effectively schedules evictions on purpose, for no real gain.
There isn’t a good reason to use this setting; we literally cannot theory-craft a hypothetically useful situation. It exists only for backward compatibility at the moment. We mention the setting in this book only since, sadly, it has been recommended in various articles on the Internet as a good performance tip.
It is not. Never use it!
Monitoring fielddataedit
It is important to keep a close watch on how much memory is being used by fielddata, and whether any data is being evicted. High eviction counts can indicate a serious resource issue and a reason for poor performance.
Fielddata usage can be monitored:
-
per-index using the
indices-stats
API:GET /_stats/fielddata?fields=*
-
per-node using the
nodes-stats
API:GET /_nodes/stats/indices/fielddata?fields=*
- Or even per-index per-node:
GET /_nodes/stats/indices/fielddata?level=indices&fields=*
By setting ?fields=*
, the memory usage is broken down for each field.
Circuit Breakeredit
An astute reader might have noticed a problem with the fielddata size settings. fielddata size is checkedafter the data is loaded. What happens if a query arrives that tries to load more into fielddata than available memory? The answer is ugly: you would get an OutOfMemoryException.
Elasticsearch includes a fielddata circuit breaker that is designed to deal with this situation. The circuit breaker estimates the memory requirements of a query by introspecting the fields involved (their type, cardinality, size, and so forth). It then checks to see whether loading the required fielddata would push the total fielddata size over the configured percentage of the heap.
If the estimated query size is larger than the limit, the circuit breaker is tripped and the query will be aborted and return an exception. This happens before data is loaded, which means that you won’t hit an OutOfMemoryException.
The circuit breaker limits can be specified in the config/elasticsearch.yml
file, or can be updated dynamically on a live cluster:
It is best to configure the circuit breaker with a relatively conservative value. Remember that fielddata needs to share the heap with the request
circuit breaker, the indexing memory buffer, the filter cache, Lucene data structures for open indices, and various other transient data structures. For this reason, it defaults to a fairly conservative 60%. Overly optimistic settings can cause potential OOM exceptions, which will take down an entire node.
On the other hand, an overly conservative value will simply return a query exception that can be handled by your application. An exception is better than a crash. These exceptions should also encourage you to reassess your query: why does a single query need more than 60% of the heap?
In Fielddata Size, we spoke about adding a limit to the size of fielddata, to ensure that old unused fielddata can be evicted. The relationship between indices.fielddata.cache.size
and indices.breaker.fielddata.limit
is an important one. If the circuit-breaker limit is lower than the cache size, no data will ever be evicted. In order for it to work properly, the circuit breaker limit must be higher than the cache size.
It is important to note that the circuit breaker compares estimated query size against the total heap size,not against the actual amount of heap memory used. This is done for a variety of technical reasons (for example, the heap may look full but is actually just garbage waiting to be collected, which is hard to estimate properly). But as the end user, this means the setting needs to be conservative, since it is comparing against total heap, not free heap.
这种情况,主要是查询时,fielddata缓存的数据越来越多造成的(默认是不自动清理的)
{
"persistent" : {
"indices.fielddata.cache.size" : "40%"
}
}
设置这个参数,让字段数据缓存的内存大小达到heap 40%就起用自动清理旧的缓存数据
详情参见:
https://www.elastic.co/guide/en/elasticsearch/guide/current/_limiting_memory_usage.html#fielddata-size
建议内存至少2G,不然总清理内存,代价是很高。
cat一下fielddata,看看是哪些字段占用的,看看字段是否是analyzed过的,分析过的无法使用doc_value,如果还是增加,那就是你的查询很频繁了,试试限制fielddata的使用率:
{
"persistent" : {
"indices.fielddata.cache.size" : "40%"
}
}
还有就是,如果是日志类型的,一般都是查询最新的数据,可以尝试在每天半夜什么时候,clear cache一下(前提是基本前一天的查询不会经常被再查询了)
相关推荐
- **设置circuitbreaker**:解释了如何设置电路断路器以防止内存溢出。 - **IndexBuffers**:介绍了如何配置索引缓冲区以提高写入性能。 - **IndexRefreshrate (索引刷新频率)**:讨论了如何调整索引刷新频率以平衡...
- 注意内存管理:避免一次性加载过多数据,防止内存溢出。 通过以上方法,你可以使用Java API高效地操作Elasticsearch,执行检索、导入、导出和统计任务。同时,持续关注Elasticsearch的新版本和最佳实践,以提升...
- **JVM 配置**:Elasticsearch 需要 Java 运行环境,确保系统已安装 JRE 或 JDK,并且 Elasticsearch 的内存设置(如 `xms` 和 `xmx`)适中,避免内存溢出。 - **权限设置**:Elasticsearch 默认不允许非 root 用户...
- **内存管理**:为了防止内存溢出(OOM)问题,real-memory circuit breaker(>=7.0版本)可以提供更精确的内存管理。 - **数据分析与搜索能力增强**:对于需要更强大数据分析能力的应用场景,Dataframe和Dense-...
2. **文件大小限制**:处理大文件时,可能需要调整ES的内存设置,防止因文件过大导致内存溢出。 3. **安全问题**:处理敏感或隐私数据时,应确保数据安全,避免信息泄露。 总结,`ingest-attachment`插件是Elastic...
4. **监控资源使用**:防止内存溢出和磁盘空间不足。 总之,Elasticsearch 作为一款强大的搜索引擎,其灵活性、高性能和易用性使其成为现代大数据应用的理想选择。理解和掌握这些核心概念和技术,将有助于你更好地...
- **监控段内存的增长趋势**:确保倒排索引不会导致内存溢出。 - **合理配置缓存大小**:例如 field cache、filter cache 等,避免不必要的内存消耗。 - **避免大量结果集的搜索与聚合操作**:推荐使用 scan & ...
- **JVM内存设置**:根据你的系统资源调整Elasticsearch和Logstash的JVM堆大小,防止内存溢出。 - **防火墙配置**:允许必要的入站和出站流量,特别是Kibana和Elasticsearch的默认端口(分别为5601和9200)。 - **...
1. JVM参数调整:根据系统资源,合理设置JVM堆内存大小,防止内存溢出。 2. 查询优化:避免使用过于复杂的查询语句,合理利用聚合和过滤器提高查询效率。 3. 日志级别设置:调整日志级别以减少不必要的日志输出,...
当处理大量或大尺寸图片时,常规的AnimationDrawable可能会导致内存溢出,这时可以考虑使用OpenGL ES (GLS)来实现帧动画,以更高效地管理内存并提升性能。本文将详细探讨如何通过GLS实现Android帧动画的绘制。 一、...
在Android平台上,图片资源的管理是一项关键任务,因为不当处理可能会导致内存溢出,严重影响应用的性能和用户体验。本文将全面分析Android图片占用内存的情况,解答关于图片内存占用的疑惑。 首先,我们要理解...
本篇文章将深入探讨基于给定文件名的JVM异常分析主题,包括socket端口占用问题和内存溢出系统异常,以及如何解决这些问题以防止异常宕机。 首先,让我们关注"623scoket端口占有和内存溢出系统异常分析"这个文件。...
为了防止内存溢出,项目中设置了一个参数,限制了一次处理的最大提交次数。对于大型存储库,测试显得尤为重要,以确保系统在处理大量数据时的稳定性和效率。 【知识点详细说明】: 1. **GitHub API**:项目依赖于...
学习如何高效地使用Bitmap,尤其是了解其内存占用和内存缓存策略,对于防止内存泄漏至关重要。 2. **内存管理**:Android系统对内存有严格的限制,特别是对于位图资源。不恰当的Bitmap管理可能导致内存溢出,影响...
8. **内存溢出问题**:处理大量或大尺寸的Bitmap可能会导致内存溢出。学习如何计算Bitmap的内存占用,并结合系统提供的工具(如Profile GPU Rendering、Memory Profiler等)进行性能分析和调试,是避免这类问题的...
- 内存管理:通过智能缓存策略,保持常用瓦片在内存中,同时根据内存限制剔除不常用的瓦片,以防止内存溢出。 2. **安卓实现**: - 在安卓平台上,通常使用ImageView或者自定义View来显示地图块。`...
这些技巧对于处理大量图片的应用尤其重要,能有效防止内存溢出并提高应用的响应速度。 五、框架和架构设计 Gallery3D作为一个复杂的Android应用,其源码必然包含了良好的模块化和组件化设计。这包括对MVC或MVVM等...
7. **注意内存溢出** - 监控`Activity`的生命周期,确保在适当的时候释放`BitmapRegionDecoder`和解码的`Bitmap`对象,防止内存泄露。 8. **优化渲染** - 考虑使用`Hardware-accelerated`模式,提高图像绘制效率...
例如,使用LRU(Least Recently Used)算法来决定哪些图片应该保留在内存中,哪些需要被替换或者释放,以防止内存溢出并保持系统响应。 3. **多线程处理** 在Gallery3D中,图片的解码和加载通常在后台线程进行,以...
为防止内存溢出,我们需要合理设置`Bitmap`的配置。通过`BitmapFactory.Options`可以在解码时指定图片的缩放比例和格式,如`inSampleSize`用于缩小图片尺寸,`inPreferredConfig`选择合适的颜色配置,如`ARGB_8888`...