in reply to memory leak
# memory used increase 2000k, undef $Template; #the memory did not released
How are you determining the memory usage here?
If you are using a tool like ps or top to view the memory usage of the perl process, then you will get this result. When perl requests memory from the operating system via malloc, this memory is not returned until the process dies. When perl is finished with the memory is is made available (with free) in the heap so that further memory allocations via malloc will use it again until it is exhausted (at which time it will go to the operating system again for more) but it is not returned to the operating system.
Implementing a malloc-style memory allocator that does return memory to the operating system upon free would be complicated and it would be prevented from returning memory in lots of cases anyway. This is because the freed memory is likely to be surrounded by other memory that has not yet been freed, or because memory can only be freed in full pages (typically 4 or 8 kibibytes) and there is other stuff on the same page that is still required.
The worst that will happen anyway if you have a process that has freed lots of memory but the process keeps on living for a long time such that the useless memory held by the process forces the whole system to eventually run low on memory is that the chunks that have been freed will be swapped out to disk (they would be chosen as good candidates for swapping by the operating system since they have not been referenced in a long time) and will never be swapped in again. If the operating system is doing a good job then the pageout will happen a little bit before it is needed, so no process will be forced to block and wait for it. That's almost no performance penalty.
If you think instead that the memory occupied by $Template has not been freed by perl, then you must assume that someone is still holding a reference to it. That reference could be in a package variable or a toplevel-scoped lexical in the Text::Template class for example. You'd have to check the implementation. But I don't think this is what's going on here.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: memory leak
by camenix (Acolyte) on Dec 12, 2005 at 02:59 UTC |