2000-11-07 16:16:11

by Abel Mu?oz Alcaraz

[permalink] [raw]
Subject: A question about memory fragmentation

Hi everybody,

Thank you for your help!
I am going to tell you more information.

my question is about memory fragmentation when I allocate and free a lot of
small memory pieces in a kernel module.
Can it do a memory fragmentation problem?
Can I solve it using 'linux/list.h' API?

I think that is better to allocate a big piece of memory and get the nodes
from this buffer with my own memory management functions; Is this correct?.

Thanks.

Abel Mu?oz Alcaraz.
Media Security Software Developer.
mailto:[email protected]
Trymedia Systems


2000-11-07 16:40:37

by Francis Galiegue

[permalink] [raw]
Subject: Re: A question about memory fragmentation

On Tue, 7 Nov 2000, Abel Mu?oz Alcaraz wrote:

>
> my question is about memory fragmentation when I allocate and free a lot of
> small memory pieces in a kernel module.
> Can it do a memory fragmentation problem?
> Can I solve it using 'linux/list.h' API?

Fragmentation is not really an issue there. Linux maintains a pool of pages of
different sizes, and you will be allocated a page with the next size superior
to what you ask for. Sizes are all powers of two, so the worst case would be
that you ask 2^n+1 bytes and get a 2^(n+1) size page. When you free such a
page, it is merged if needed with a coalescent page and put back in the "upper
size" pool (not sure about this, though). So, no fragmentation is to be feared.

As to whether you use list.h or not, it really doesn't have any influence,
except that the list.h implementation is the preferred and standard way to make
doubly linked lists under Linux.

So, no, there's no need to allocate one big pool and manage it yourself. Also
remember that you can only kmalloc() up to 128k - if you want to allocate
bigger amounts of memory, use vmalloc().

Another solution: create your own slab cache for your objects.

--
Francis Galiegue, [email protected]
"Programming is a race between programmers, who try and make more and more
idiot-proof software, and universe, which produces more and more remarkable
idiots. Until now, universe leads the race" -- R. Cook

2000-11-07 16:42:37

by Alan

[permalink] [raw]
Subject: Re: A question about memory fragmentation

> I think that is better to allocate a big piece of memory and get the n=
> odes
> from this buffer with my own memory management functions; Is this corre=
> ct?.

See the SLAB interface. It'll do that for you. Kmalloc uses SLAB so will do
similarly sane things