# Java heap space when using unique count on specific field

**URL:** https://discuss.elastic.co/t/java-heap-space-when-using-unique-count-on-specific-field/79047
**Category:** Elasticsearch
**Created:** [March 17, 2017, 3:03pm UTC](https://discuss.elastic.co/t/java-heap-space-when-using-unique-count-on-specific-field/79047 "2017-03-17T15:03:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![madleeen](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@madleeen](https://discuss.elastic.co/u/madleeen)
#### Post date: [March 17, 2017, 3:03pm UTC](https://discuss.elastic.co/t/java-heap-space-when-using-unique-count-on-specific-field/79047/1 "2017-03-17T15:03:14Z")

</div>

Hello,  
first of all, I am very new to Elasticsearch, which could be the main problem 🙂 .  
**My settings:**  
I am running single node Elasticsearch (5.2.2) and Kibana (5.2.2) in Docker containers. I also tried the same with 5.1.x (with the same result).  
I have one index (let's say "my\_index") with around 20 million documents (~7GB).  
**My problem:**  
I was running this on version 2 without bigger troubles, then upgraded and since then I suffer a lot from Java Heap Space - Out Of Memory.  
My logs (docker logs -f elasticsearch) are full of warning about garbage collector: e.g. [gc][108] overhead, spent [1s] collecting in the last [1.2s]  
At first I thought it will be just too low heap (althought machine where ES v2 was running had only 8GB), so I started to experimenting with it.  
Right now it is set to 24GB (-Xms24G -Xmx24G). No matter what I set, it always ends in OOM after one specific attempt to create vizualization in Kibana.  
As I've heard default settings of both Kibana and ES are quite all right, so I didn't change anything (although I was experimenting with it with no luck).  
Example documents looks like this:

> {  
> "\_index" : "my\_index",  
> "\_type" : "event",  
> "\_id" : "someID",  
> "\_version" : 1,  
> "found" : true,  
> "\_source" : {  
> "time" : {  
> "observation" : "2016-03-12T13:44:38+00:00",  
> "source" : "2016-03-12T13:58:30+00:00"  
> },  
> "feed" : {  
> "url" : "[http://xxxx](http://xxxx)",  
> "accuracy" : 20.0,  
> "name" : "some\_name"  
> },  
> "raw" : "some\_hase",  
> "classification" : {  
> "type" : "",  
> "taxonomy" : ""  
> },  
> "source" : {  
> "geolocation" : {  
> "city" : "city",  
> "cc" : "country-code",  
> "latitude" : 154,  
> "longitude" : 254  
> },  
> "asn" : 1234,  
> "ip" : "255.255.255.255",  
> "network" : "255.255.255.255/12"  
> }  
> }  
> }

My problem is that I can't use unique count with source.ip for example in Kibana as Vizualize -\> Histogram, where I want to have on Y-axis Unique count of field source.ip and X-ax shoud be source.ip with subbucket of feed name. Result shows which source.ip came to me from the most sources.  
In general unique count is in my case problematic.

Whenever I try it, Kibana shows:  
"Error: Request Timeout after 30000ms  
at http://kibana\_URL/bundles/kibana.bundle.js?v=14723:14:8629  
at http://kibana\_URL/bundles/kibana.bundle.js?v=14723:14:9050"

This shows in logs (docker logs -f elasticsearch):  
[http://pastebin.com/Eejst5g3](http://pastebin.com/Eejst5g3)  
And also this:

> **[\[2017-03-20T10:12:24,559\]\[ERROR\]\[o.e.x.m.AgentService \] \[9OFRpS4\]...](https://pastebin.com/QVLB9ERA)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

This is settings of my index:  
"settings" : {  
"index" : {  
"refresh\_interval" : "5s",  
"number\_of\_shards" : "1",  
"provided\_name" : "my\_index",  
"number\_of\_replicas" : "0",  
"version" : {  
"created" : "5010299",  
"upgraded" : "5020299"  
}

Healh of index:  
"my\_index" : {  
"status" : "green",  
"number\_of\_shards" : 1,  
"number\_of\_replicas" : 0,  
"active\_primary\_shards" : 1,  
"active\_shards" : 1,  
"relocating\_shards" : 0,  
"initializing\_shards" : 0,  
"unassigned\_shards" : 0  
},

What should I try to fix this? Could anybody help, advice? I will provide additional information if needed.  
Thx

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 20, 2017, 11:38am UTC](https://discuss.elastic.co/t/java-heap-space-when-using-unique-count-on-specific-field/79047/2 "2017-03-20T11:38:28Z")

</div>

The problem is caused by having many unique buckets (source IPs) and trying to count exactly how many things fall into each of these buckets. We can reduce the memory overhead by relaxing the need to count exactly.

The unique-count part of your query is executed under the covers by the `cardinality` aggregation [1] and by default it counts things accurately up to 3,000 unique values and then flips into a fuzzier (potentially inaccurate) mode for counting more values but using less memory. 3,000 unique values can take a lot of memory for each of the source\_ips so we can reduce the memory cost but sacrifice some accuracy by reducing the `precision_threshold` setting to something significantly less than 3,000.

I had a similar query (not `source-ips -> num feeds` but `reviewers-> num sellers`) which also ran out of memory using a visualization like yours. By changing the "advanced" setting on the y-axis unique sellers count as in this screenshot I prevented the memory issue:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/f/2/f297014e711f5372a3ec64a23a88ab7169719d7d.png)

Tuning the numbers for your dataset will be walking a tricky line between getting accurate results and not running out of memory. Recognise that this is a challenging request to run in a single pass using aggregations on an event-centric index and sometimes[entity-centric indexes](https://www.youtube.com/watch?v=yBf7oeJKH2Y) make more sense when studying the behaviour of entities.

Cheers  
Mark

[1] [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 17, 2017, 11:38am UTC](https://discuss.elastic.co/t/java-heap-space-when-using-unique-count-on-specific-field/79047/3 "2017-04-17T11:38:30Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
