Hi, hackers ! TopMemoryContext is created with malloc in MemoryContextInit(). All the other MemoryContexts such as ErrContext, MessageContext are allocated with palloc in a block of TopMemoryContext. So, to release all the allocated memories, TopMemoryContext should be freed with MemoryContextReset and freeing itself. But, I cannot find the place where it happens in source files. To check if TopMemoryContext is released before exiting a process, I did some work like below and checked the result. 1. Add some codes in src/backend/tcop/postgres.c ----------------------------------------------------------- void PostgresMain(...) { ...... for(;;) { .... switch(firstchar) { .... case 'X' : case EOF : ... ereport(DEBUG2,(errmsg("************ process(%d) is about to exit. ***************",getpid()))); proc_exit(0); } } } -------------------------------------------------------- Later, I am going to run psql as a backend process. When psql quits itself, ereport would leave a debug message. 2. Add some codes in src/backend/storage/ipc/ipc.c ----------------------------------- ... #include "utils/memutils.h" ... void proc_exit(..) { .... elog(DEBUG3, "exit(%d)", code); } ----------------------------------- MemoryContextStats will leave message about the stats of TopMemoryContext just before a process is terminated. 3. compile and install 4. start postgresql server postgres -d 5 -D /usr/local/pgsql/data 5. run psql and quit psql test test=# \q ---------------------------- DEBUG: forked new backend, pid=7967 socket=8 --------------------------- From the message above, TopMemoryContext has 94blocks of memory until just before exit is called. Then, when or where is TopMemoryContext released ?? |