# Memory usage increases over time

**URL:** <https://community.particle.io/t/memory-usage-increases-over-time/58277>\
**Category:** Firmware\
**Created:** [November 2, 2020, 10:25am UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277 "2020-11-02T10:25:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SmartWater](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@SmartWater](https://community.particle.io/u/SmartWater)\
**Post date:** [November 2, 2020, 10:25am UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/1 "2020-11-02T10:25:05Z")

</div>

Hey,

i have an E-Series Dev board.

The initial memory usage is around 30%. After 24h it starts to increase as show on the graph:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/d/3/d31b4376107c0ce9be492e3807f150f10de5dfec.png)

Any idea whats leads to this behaviour?  
I am putting the board to 30s sleep, wake up read sensors, send to the cloud and then sleep again.

Thanks

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [November 2, 2020, 12:50pm UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/2 "2020-11-02T12:50:27Z")

</div>

It appears that you have a memory leak; somewhere you are allocating memory, such as from new, malloc, strdup, or other functions that allocate memory, and are not freeing it (delete or free).

Without your code, it’s impossible to say where it might be.

---

<div class="post-metadata">

**Author:** ![SmartWater](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@SmartWater](https://community.particle.io/u/SmartWater)\
**Post date:** [November 2, 2020, 4:46pm UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/3 "2020-11-02T16:46:52Z")

</div>

I think this is the part in my program causing it

```auto
    ubidots.addContext("system_status", strdup(system_status.c_str()));
    char* context = (char *) malloc(sizeof(char) * 60);
    ubidots.getContext(context);
    ubidots.add("system_status", 0, context);
    free(context);

```

I am freeing the malloc part, but the strdup() could cause it. Free up the duplication?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [November 2, 2020, 5:14pm UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/4 "2020-11-02T17:14:39Z")

</div>

`strdup()` internally already allocates the required buffer to fit the copy in and you would need to catch the pointer returned by it to `free()` that memory again.

But even after getting rid of the memory leak you may run into heap fragmentation issues over time.

When you have these lines in a function and the string lengths are not excessive I’d rather go with automatic variables.  
e.g.

```cpp
  char context[60];
  char stat[system_status.length() + 1];
  system_status.toCharArray(stat, sizeof(stat));
  ...

```

---

<div class="post-metadata">

**Author:** ![SmartWater](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@SmartWater](https://community.particle.io/u/SmartWater)\
**Post date:** [November 3, 2020, 10:56am UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/5 "2020-11-03T10:56:14Z")

</div>

Hey,  
so i changed it to the following:

```auto
    char stat[system_status.length() + 1];
    char context[60];
    ubidots.addContext("system_status", system_status.toCharArray(stat, sizeof(stat)));
    ubidots.getContext(context);
    ubidots.add("system_status", 0, context);

```

now i get an error: invalid use of void expression

any suggestiongs?  
Thanks

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [November 3, 2020, 12:20pm UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/6 "2020-11-03T12:20:13Z")

</div>

`toCharArray()` does _ **not** _ return a pointer.  
[https://docs.particle.io/reference/device-os/firmware/photon/#tochararray-](https://docs.particle.io/reference/device-os/firmware/photon/#tochararray-)

What you want is all of my code above as is _ **plus** _

```cpp
 ubidots.addContext("system_status", stat);

```

_(I thought that was obvious, hence the “…” for you to fill in the blanks 😉 )_

---

<div class="post-metadata">

**Author:** ![SmartWater](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@SmartWater](https://community.particle.io/u/SmartWater)\
**Post date:** [November 3, 2020, 12:39pm UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/7 "2020-11-03T12:39:37Z")

</div>

Thanks,

that was it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/3/2/3291e3c9351789fe8620ae5bcec8d3fec67ff221.png) [@system](https://community.particle.io/u/system)\
**Post date:** [May 7, 2021, 12:13am UTC](https://community.particle.io/t/memory-usage-increases-over-time/58277/9 "2021-05-07T00:13:41Z")

</div>

Closed on May 7, 2021, 12:13am UTC

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