Marc SCHAEFER wrote:
> Tiens, une question pour sp�cialiste C++: l'impl�mentation Linux
> impl�mente-t-elle un free() apr�s un op�rateur del sur un objet sans
> r�f�rence, ou la m�moire n'est pas rendue ?
A priori, en C++, qu'il reste ou non des r�f�rences ne change rien.
Je ne sais pas si �a passe par un free(), mais ceci :
#include <stdlib.h>
int main()
{
const size_t sz = 1024*1024;
for (int r = 0; r < 100; ++r) {
char * const pc = new char [sz*r];
for (size_t i = 0; i < sz*r; i+=r)
pc[i] = 'm';
delete[] pc;
}
}
donne une strace du genre :
old_mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x401a2000
munmap(0x401a2000, 1052672) = 0
old_mmap(NULL, 2101248, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x401a2000
munmap(0x401a2000, 2101248) = 0
old_mmap(NULL, 3149824, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x401a2000
munmap(0x401a2000, 3149824) = 0
old_mmap(NULL, 4198400, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x401a2000
munmap(0x401a2000, 4198400) = 0
old_mmap(NULL, 5246976, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x401a2000
> Si ce free() n'est jamais fait, cela expliquerait pourquoi, m�me en C++,
> on a int�r�t � impl�menter malloc() et free() pour des allocations de
> m�moire cons�quentes.
Mais la m�moire est effectivement rendue.
Je n'avais jamais entendu parl� de ce probl�me, mais je n'ai pas non chercher dans ce
sens, as-tu des r�f int�ressantes ?
> (sym�triquement, free() ne rend pas la m�moire au syst�me mais au pool du
> processus, bien souvent, pour des questions de fragmentation).
Sauf erreur la libc utilise mmap pour les allocations cons�quentes.
Efficiency Considerations for `malloc'
......................................
As opposed to other versions, the `malloc' in the GNU C Library does
not round up block sizes to powers of two, neither for large nor for
small sizes. Neighboring chunks can be coalesced on a `free' no matter
what their size is. This makes the implementation suitable for all
kinds of allocation patterns without generally incurring high memory
waste through fragmentation.
Very large blocks (much larger than a page) are allocated with
`mmap' (anonymous or via `/dev/zero') by this implementation. This has
the great advantage that these chunks are returned to the system
immediately when they are freed. Therefore, it cannot happen that a
large chunk becomes "locked" in between smaller ones and even after
calling `free' wastes memory. The size threshold for `mmap' to be used
can be adjusted with `mallopt'. The use of `mmap' can also be disabled
completely.
Marc Mongenet
--
http://www-internal.alphanet.ch/linux-leman/ avant de poser
une question. Ouais, pour se d�sabonner aussi.