Which MemoryContext?

Started by Gevik Babakhanialmost 18 years ago7 messages
#1Gevik Babakhani
pgdev@xs4all.nl

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.

#2Heikki Linnakangas
heikki@enterprisedb.com
In reply to: Gevik Babakhani (#1)
Re: Which MemoryContext?

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

#3Gevik Babakhani
pgdev@xs4all.nl
In reply to: Heikki Linnakangas (#2)
Re: Which MemoryContext?

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.

#4Heikki Linnakangas
heikki@enterprisedb.com
In reply to: Gevik Babakhani (#3)
Re: Which MemoryContext?

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

#5Gevik Babakhani
pgdev@xs4all.nl
In reply to: Heikki Linnakangas (#4)
Re: Which MemoryContext?

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.

#6Alvaro Herrera
alvherre@commandprompt.com
In reply to: Gevik Babakhani (#5)
Re: Which MemoryContext?

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

#7Gevik Babakhani
pgdev@xs4all.nl
In reply to: Alvaro Herrera (#6)
Re: Which MemoryContext?

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