lwip-users
[Top][All Lists]
Advanced

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

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


From: address@hidden
Subject: Re: [lwip-users] lwip_cyclic_timer() repeatedly calling malloc/free
Date: Thu, 3 Feb 2022 17:54:15 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0

Am 01.02.2022 um 15:39 schrieb R. Diez via lwip-users:
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

That's a valid configuration, but not the one most often used, as malloc
is often much slower than our pool allocation.


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.

That could be implemented, but it's not so far. We're open to patches
doing that though.

However, when using pool allocation, performance is not a real issue
here. And the number of timers used is pretty static. You'll know how
much you need. Even if you allocate a few more, timers aren't the high
memory consuming factor in your system.

I can see that you wouldn't want one full-malloc call per timer though...

Regards,
Simon


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]