# Memory issue during stream operation

**URL:** <https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033>\
**Category:** Ristretto\
**Tags:** kind:question, kind:bug\
**Created:** [March 2, 2021, 8:48pm UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033 "2021-03-02T20:48:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgokden](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/bgokden/32/5460_2.png) [@bgokden](https://discuss.dgraph.io/u/bgokden)\
**Post date:** [March 2, 2021, 8:48pm UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/1 "2021-03-02T20:48:18Z")

</div>

### What version of Go are you using (`go version`)?

```
$ go version
1.16.0
```

### What operating system are you using?

Linux. It is happening inside docker image: [gcr.io](http://gcr.io) \ distroless \ static@sha256:c6d5981545ce1406d33e61434c61e9452dad93ecd8397c41e89036ef977a88f4

### What version of ristretto are you using?

v3.2011.1

### Does this issue reproduce with the latest master?

v3.2011.1 is the latest stable version.

### Steps to Reproduce the issue

Run the application and wait until it is killed as Out of memory.

### What ristretto options were set?

Default options. Only path and log level are set.

### What did you do?

My application is running a stream operation regularly that scans iterates values and my application is killed due to memory after some period. I checked pprof output:

```auto
(pprof) top
Showing nodes accounting for 531.20MB, 99.08% of 536.13MB total
Dropped 19 nodes (cum <= 2.68MB)
Showing top 10 nodes out of 18
      flat flat% sum% cum cum%
     448MB 83.56% 83.56% 448MB 83.56% github.com\dgraph-io\ristretto\z.Calloc (inline)
   83.20MB 15.52% 99.08% 83.20MB 15.52% github.com\dgraph-io\badger\v3\skl.newArena (inline)
         0 0% 99.08% 85MB 15.85% github.com\bgokden\veri\data.(*Data).InitData
         0 0% 99.08% 85MB 15.85% github.com\bgokden\veri\data.(*Dataset).CreateIfNotExists
         0 0% 99.08% 85MB 15.85% github.com\bgokden\veri\data.(*Dataset).GetOrCreateIfNotExists
         0 0% 99.08% 85MB 15.85% github.com\bgokden\veri\node.(*Node).CreateDataIfNotExists
         0 0% 99.08% 85MB 15.85% github.com\bgokden\veri\veriservice._VeriService_CreateDataIfNotExists_Handler
         0 0% 99.08% 83.20MB 15.52% github.com\dgraph-io\badger\v3.(*DB).newMemTable
         0 0% 99.08% 83.20MB 15.52% github.com\dgraph-io\badger\v3.(*DB).openMemTable
         0 0% 99.08% 448MB 83.56% github.com\dgraph-io\badger\v3.(*Stream).Orchestrate.func1

```

Most of the memory is being used by [github.com/dgraph-io/ristretto/z.Calloc](http://github.com/dgraph-io/ristretto/z.Calloc)  
I don’t know if there is a way to free it.

### What did you expect to see?

This memory is freed after some period.

### What did you see instead?

Continuously increasing memory.

Sorry I had to convert all slashes with reverse slash.

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [March 3, 2021, 5:10am UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/2 "2021-03-03T05:10:34Z")

</div>

I’m trying to understand: is this a problem with Badger that you are facing?

Can you tell me what your application is doing ?

---

<div class="post-metadata">

**Author:** ![ibrahim](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ibrahim/32/6387_2.png) [@ibrahim](https://discuss.dgraph.io/u/ibrahim)\
**Post date:** [March 3, 2021, 1:26pm UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/3 "2021-03-03T13:26:40Z")

</div>

@bgokden can you try to run badger with jemalloc?  
You will have to install jemalloc locally. Please see this

> <https://github.com/dgraph-io/dgraph/blob/c4c3248db7a2a914171850430953c1890e3d7b75/dgraph/Makefile#L92-L108>

And then, you’ll have to build your go program with jemalloc. To do that, run `go build -tags=jemalloc ... `.

You should see a significant improvement in badger’s memory utilization with jemalloc

---

<div class="post-metadata">

**Author:** ![mrjn](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrjn/32/775_2.png) [@mrjn](https://discuss.dgraph.io/u/mrjn)\
**Post date:** [March 4, 2021, 9:55am UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/4 "2021-03-04T09:55:17Z")

</div>

@ibrahim can we create a makefile for Badger? Or document in Readme about jemalloc?

---

<div class="post-metadata">

**Author:** ![bgokden](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/bgokden/32/5460_2.png) [@bgokden](https://discuss.dgraph.io/u/bgokden)\
**Post date:** [March 4, 2021, 8:17pm UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/5 "2021-03-04T20:17:49Z")

</div>

This looks like a solution that I can apply. I will try and reply here.

---

<div class="post-metadata">

**Author:** ![bgokden](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/bgokden/32/5460_2.png) [@bgokden](https://discuss.dgraph.io/u/bgokden)\
**Post date:** [March 4, 2021, 10:30pm UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/6 "2021-03-04T22:30:27Z")

</div>

I think this can be called the solution.  
I added jemalloc and compiled with jemalloc.  
I was using an alpine image but the current alpine doesn’t have support so I moved to debian image.  
Also, I was compiling with CGO\_ENABLED=0, removed it.  
Base memory usage of the application increased but it is stable now.  
I was trying to keep my application with no C dependencies and pure go.

Thank you for showing me the right direction.

---

<div class="post-metadata">

**Author:** ![ibrahim](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ibrahim/32/6387_2.png) [@ibrahim](https://discuss.dgraph.io/u/ibrahim)\
**Post date:** [March 5, 2021, 6:41am UTC](https://discuss.dgraph.io/t/memory-issue-during-stream-operation/13033/7 "2021-03-05T06:41:28Z")

</div>

> @ibrahim can we create a makefile for Badger? Or document in Readme about jemalloc?

Badger is used as a library so the users won’t benefit even if there was a makefile. We can document this in the README file and badger’s documentation.

I’ll get it done.
