lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] lwip_cyclic_timer() repeatedly calling malloc/free


From: R. Diez
Subject: [lwip-users] lwip_cyclic_timer() repeatedly calling malloc/free
Date: Tue, 1 Feb 2022 15:39:37 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0

Hi all:

I have a few embedded bare-metal applications that use lwIP. The 
microcontrollers tend to have limited SRAM, but it is usually enough to use 
malloc() with care for non-critical tasks.

I guess I could investigate about lwIP memory options and starting using pools, 
but I am unsure about how much memory to reserve for that purpose. At the 
moment, I am just using malloc for everything with these settings:

#define MEM_LIBC_MALLOC 1
#define MEMP_MEM_MALLOC 1

I often keep an eye on malloc() usage, because it can be a source of problems: 
excessive heap usage, potential memory leaks, performance degradation, memory 
exhaustion due to fragmentation...

I have realised that lwIP allocates and releases memory often even if it has 
nothing to do. This is the call stack:

#2  malloc()
#3  mem_malloc()
#4  do_memp_malloc_pool_fn()
#5  memp_malloc_fn()
#6  sys_timeout_abs()
#7  lwip_cyclic_timer()
#8  sys_check_timeouts()

Routine lwip_cyclic_timer() has the following comment:

"Timer callback function that calls cyclic->handler() and reschedules itself."

I have looked at the lwIP code, and it seems that every time a timer expires, 
it releases its memory, and every time that a timer wants to reschedule itself, 
it allocates memory again.

Is there a way to prevent that? I would have thought that a timer that always 
reschedules itself could just reuse its allocated memory over and over.

This is not just an issue with malloc usage. I think that, even if I switched 
lwIP to memory pools, allocating and releasing memory from some pool so often 
is probably not desirable either, because at the end of the day, it is more or 
less equivalent to malloc/free. I could probably create a separate pool for 
timers with MEMP_SYS_TIMEOUT, but then you would have to know how many timers 
lwIP will be needing, and that can be tricky to predict. In any case, setting 
memory aside only for timers may increase memory waste for those scenarios 
where lwIP may not be actually used much if another communication channel is 
available. Or did I miss some cool trick in lwIP pool management?

Thanks in advance,
  rdiez


reply via email to

[Prev in Thread] Current Thread [Next in Thread]