Memory usage increases over time

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:

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

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.

2 Likes

I think this is the part in my program causing it

    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?

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.

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

Hey,
so i changed it to the following:

    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

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

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

 ubidots.addContext("system_status", stat);

(I thought that was obvious, hence the β€œβ€¦β€ for you to fill in the blanks :wink: )

Thanks,

that was it.