In-memory garbage collection

I am heavily using RAM allocating space for small data chunks using malloc() and free(). There must be no memory leaks, at least known to me. But sometimes I see significant free RAM size decrease, the size may fluctuate significantly. There's currently a need to increase using memory allocations, and I am looking to identify the risks and overheads related to it.

Question: how in-RAM data garbage collection works, and is there any way for application to manage it (e.g. request garbage collection when it has idle time).

Thank you.

If you are using malloc and free, then garbage collection is entirely up to you...

Anyway, how is this related to OpenWrt, and not a generic application development question?

2 Likes

Thanks for your response.

I am currently developing for openwrt, and need to know how things are done here.

malloc is expected to find free space in RAM and return pointer to it, free is expected to release that space to the heap. But algorithm on managing the selection process of the newly allocated RAM space is not under application control, as well as management of the fragmentation of the RAM.

As I said before my problem is decreasing free RAM space - it may happen due to memory leak, or due to inefficient garbage collection/RAM fragmentation.

By the way, is there any application showing chain/list of the allocated blocks in the RAM and what application/thread they belong to?

(some theory here)

There is nothing special about OpenWrt here. If you write C code, you are the garbage collector - whatever you miss, will leak.

3 Likes

So you definitely say that if I perform malloc and free, then size of RAM displayed by, e.g. free, will never change (significantly)? And if I have started the system with 40 MB free RAM, and in several hours free shows 10 MB, this is only because of memory leak - in other words someone/something in the system did not perform free?

Can I somehow see the chain of the allocated blocks, ideally with their owners? How system-wide memory leaks are troubleshot?

the system does absolutely NOTHING during those hours ?

no traffic, to it, nor though it ?
no daemons running ?
no cron jobs firing ?
PID haven't increased at all ?

2 Likes

Thee is nothing special about how OpenWrt handles memory, this is just the Linux kernel at work. You will find lot's of info about how Linux manages memory, and why decreasing free memory is not (necessarily) an issue.

1 Like

From useful perspective it does nothing - my app is down and anything running there is out of my direct control and considered as "operating system". Of course the operating system is running, some services are running, no cron jobs. After anything finishes, it is expected to free memory it has allocated, right? Did I understand properly that it is either space allocated for traffic was not freed, or some daemon leaks the RAM? If it was 40 MB and in some hours it is 10 MB there is a risk it will get out of RAM in some time, and stop working at all.

I do not think our conversation goes in right direction. Do you have any plan on how to troubleshoot this problem? ps does not show memory allocated for processes, no idea how to see who owns the memory and therefore localize the problem of diminishing RAM space.

you're asking us what to do with your black box hw, running some black box code, on a black box OS ?

I would not agree to this. see here, there's no -o option in openwrt for ps. And there're more differences with other versions of Linux, with most advices found on the internet not applicable to the openwrt.

default ps is based on busybox, pretty sure you can install "proper" ps, if you feel like doing so, or perhaps enable more features in the busybox ps.

2 Likes

On one side, you have the Linux kernel, handling memory. On the other side, you have the user utility that shows running processes.

1 Like

Thanks. Any other suggestions you may propose?

As @eduperez already pointed out, decreasing free ram isn't necessarily a mem leak.

1 Like

Can you list other causes for it? how it may happen same software is running for hours, this software performs same tasks, but it needs more and more RAM? This means software takes more and more tasks to do without freeing the RAM without finishing tasks it has taken.

Some info about this: Linux ate my RAM!.

2 Likes

I hope it is an answer, however I am still keen to troubleshoot it deeper to ensure there's no app leaking the RAM. Because data in 'available' sometimes very low. I will monitor that and then we will see.

Thank you all!

1 Like

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