# Orphan memory in KDB process using Rserve

**URL:** <https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [March 28, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533 "2023-03-28T00:00:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![muneishadya](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@muneishadya](https://forum.kx.com/u/muneishadya)\
**Post date:** [March 28, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/1 "2023-03-28T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/orphan-memory-in-kdb-process-using-rserve](https://learninghub.kx.com/forums/topic/orphan-memory-in-kdb-process-using-rserve)

Hi Everyone,

our team is one of the users of Rserve for integrating KDB with R. Recently we noticed that one of our processes working with a R server was bloating in memory. Oddly, the .Q.w did not show any bloating in memory and .Q.gc didnt release any memory either. However the OS view of the memory consumed by the process was high and different from KDB view of its own memory.

We dug bit deeper and found that this was specifically happening in RSet function of the library.

While we are unable to view the implementation of Rserve at [https://code.kx.com/q/interfaces/r/#remote-r-rserve](https://code.kx.com/q/interfaces/r/#remote-r-rserve")I believe we are missing de-allocating memory in Rset function. Can someone help in fixing this issue or provide more insight.

&nbsp;

regards,

Muneish

---

<div class="post-metadata">

**Author:** ![muneishadya](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@muneishadya](https://forum.kx.com/u/muneishadya)\
**Post date:** [May 29, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/2 "2023-05-29T00:00:00Z")

</div>

Hi there

the memory bloating problem is with the kdb process and not the R process.

whenever the q process calls Rset there is orphan memory on the q process.

Also, .Q.gc does not release any memory. The q process and OS have different view on the amount of memory being consumed by the q process .in the end the q process shuts down even though it thinks it has low memory foot print.

&nbsp;

---

<div class="post-metadata">

**Author:** ![muneishadya](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@muneishadya](https://forum.kx.com/u/muneishadya)\
**Post date:** [May 16, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/3 "2023-05-16T00:00:00Z")

</div>

Also, need to add that we are using a file called as rserve.so , I believe its not part of embedR project.

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [May 16, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/4 "2023-05-16T00:00:00Z")

</div>

[https://github.com/KxSystems/cookbook/tree/master/r/rserve](https://github.com/KxSystems/cookbook/tree/master/r/rserve")

You are only looking at the memory usage of the q process.

With Rserve R is a separate process. You need to monitor it’s memory usage also.

&nbsp;

To free memory in q after creating a large object use `.Q.gc`. You can also start q with `-g 1`.

- [https://code.kx.com/q/basics/cmdline/#-g-garbage-collection](https://code.kx.com/q/basics/cmdline/#-g-garbage-collection")
- [https://code.kx.com/q/ref/dotq/#gc-garbage-collect](https://code.kx.com/q/ref/dotq/#gc-garbage-collect")

---

<div class="post-metadata">

**Author:** ![muneishadya](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@muneishadya](https://forum.kx.com/u/muneishadya)\
**Post date:** [May 11, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/5 "2023-05-11T00:00:00Z")

</div>

Hi Rocuinneagain ,

1. I believe it is embedR project indeed.

2. yes we are using the latest code.

3.

Rset:`rserve 2:(`rsset;2);

meminfo:{(5#system"w"), 1024\*"J"$system "ps -eo size -h -q ",string .z.i};

m1:meminfo[];

RSet["temp";100000?100];

m2:meminfo[];

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [May 12, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/6 "2023-05-12T00:00:00Z")

</div>

You can delete R variables and run R garbage collection using `Rcmd` as above.

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [May 11, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/7 "2023-05-11T00:00:00Z")

</div>

The src is available at [https://github.com/KxSystems/embedR/blob/fa5101b64e15f9ba0aa5c20affc0cd041fb41bc0/src/rserver.c#L458](https://github.com/KxSystems/embedR/blob/fa5101b64e15f9ba0aa5c20affc0cd041fb41bc0/src/rserver.c#L458")

&nbsp;

Have you tried calling

Rcmd “rm(temp)” Rcmd “gc()”

&nbsp;

&nbsp;

q does not manage the memory for R - you must still delete variables and call garbage collection

---

<div class="post-metadata">

**Author:** ![muneishadya](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@muneishadya](https://forum.kx.com/u/muneishadya)\
**Post date:** [May 12, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/8 "2023-05-12T00:00:00Z")

</div>

Currently we reassign the same variable with new data, will just calling gc resolve the bloating in that case?

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [March 28, 2023, 12:00am UTC](https://forum.kx.com/t/orphan-memory-in-kdb-process-using-rserve/13533/9 "2023-03-28T00:00:00Z")

</div>

1. Is it the embedR project that your are using?
2. Are you on the latest release of the code? [https://github.com/KxSystems/embedR/releases](https://github.com/KxSystems/embedR/releases")
3. Is it possible share a small generic piece of code to reproduce the issue?
