Thread: Which MemoryContext?

Which MemoryContext?

From
"Gevik Babakhani"
Date:
Hi,

I want to keep an array of localized strings in memory. 
This array is dynamically allocated and is going to be used between
transactions (that are not necessarily nested). 
It must be cleaned/freed when postmaster exists. 
In which context should this array be initialized? TopMemoryContext perhaps?

Any thoughts?

Regards,
Gevik.



Re: Which MemoryContext?

From
"Heikki Linnakangas"
Date:
Gevik Babakhani wrote:
> I want to keep an array of localized strings in memory. 
> This array is dynamically allocated and is going to be used between
> transactions (that are not necessarily nested). 
> It must be cleaned/freed when postmaster exists. 
> In which context should this array be initialized? TopMemoryContext perhaps?

TopMemoryContext sounds right. Be careful not to leak there.

--   Heikki Linnakangas  EnterpriseDB   http://www.enterprisedb.com


Re: Which MemoryContext?

From
"Gevik Babakhani"
Date:
> TopMemoryContext sounds right. Be careful not to leak there.

Thank you :)

I have allocated memory using: MemoryContextAlloc(TopMemoryContext,n *
sizeof(char*));
In pgsql/src/backend/utils/mmgr/README:142 is stated that memory allocated
using above should be freed manually, Is this correct? Or does the system
release everything allocated in TopMemoryContext automatically when exiting?

I looked around and found examples where memory allocated 
using above is not freed! (datetime.c:3811,   uhhh.. a bit confused here)

Any thoughts?

Regards,
Gevik.





Re: Which MemoryContext?

From
"Heikki Linnakangas"
Date:
Gevik Babakhani wrote:
> I have allocated memory using: MemoryContextAlloc(TopMemoryContext,n *
> sizeof(char*));
> In pgsql/src/backend/utils/mmgr/README:142 is stated that memory allocated
> using above should be freed manually, Is this correct? Or does the system
> release everything allocated in TopMemoryContext automatically when exiting?

On backend exit, everything in TopMemoryContext, like all other 
non-shared memory, is automatically released.

> I looked around and found examples where memory allocated 
> using above is not freed! (datetime.c:3811,   uhhh.. a bit confused here)

That palloc'd table in datetime.c is kept until backend exit, or until 
it's replaced with a new table. If it's replaced with a new table, the 
old one is explicitly pfree'd in that function:

>     /* Now safe to replace existing table (if any) */
>     if (timezonetktbl)
>         pfree(timezonetktbl);

--   Heikki Linnakangas  EnterpriseDB   http://www.enterprisedb.com


Re: Which MemoryContext?

From
"Gevik Babakhani"
Date:
> On backend exit, everything in TopMemoryContext, like all 
> other non-shared memory, is automatically released.
> 

So it is safe to not free the allocated memory in TopMemoryContext and leave
it to be released on backend exit.
Thank you for the help :)

Regards,
Gevik.




Re: Which MemoryContext?

From
Alvaro Herrera
Date:
Gevik Babakhani wrote:
> > On backend exit, everything in TopMemoryContext, like all 
> > other non-shared memory, is automatically released.
> 
> So it is safe to not free the allocated memory in TopMemoryContext and leave
> it to be released on backend exit.

All local memory is safe to handle that way.  The problem only arises
when you have memory to release _earlier_ than that.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


Re: Which MemoryContext?

From
"Gevik Babakhani"
Date:
> All local memory is safe to handle that way.  
In my case I am reallocating memory for a global variable between
transactions. I wanted to make sure I don't leave allocated memory behind.

> The problem only arises when you have memory to release _earlier_ than
that.
First I was looking for a way to free my allocated memory before exit. I
found the proc_exit hook mechanism but I am not sure where to setup this
hook.

Given allocating memory in TopMemoryContext is the same as malloc, then I'll
just leave my allocated memory to be freed automatically when exit.

Thank you :)

Regards,
Gevik