Avatar of pralteb
pralteb

asked on 

Dynamic Memory Allocation by Threads

I am developing a small client-server database system using Linux and C with Pthreads on the server side.  Each request from the client is processed by a short-lived handler thread that accesses the database and returns data to the client.

My question involves dynamic memory allocation.  Each program thread builds internal data structures using repeated calls to malloc().  After the thread terminates I assume this memory is de-referenced and left intact on the heap.

Should the thread free this memory explicitly after use?  If so, what would be the consequences of not doing so?
C

Avatar of undefined
Last Comment
evilrix
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> Should the thread free this memory explicitly after use?
Yes, if you don't free it and the memory is not longer referenced it'll leak.

>> If so, what would be the consequences of not doing so?
You'll leak memory and eventually you'll probably run out of resource (if the app is run long enough).
Avatar of pralteb
pralteb

ASKER

Wow!  That was a fast response!

I know that in a non-threaded environment I should free memory after use, but I'm not clear whether in a threaded environment the termination of the thread implicitly frees all dynamic memory allocated by that thread, saving me from doing it explicitly.  Are you saying I still need to free it explicitly even though the thread no longer exists?
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

BTW: Ratehr than spawning and killing threads it might be more efficient to maintain a  thread pool. If you do this you won't have the cost of spawning new threads. Also, you could reuse the memory in each thread rather than freeing it and then having to reallocate it for each new thread. Since heap allocation is very slow this would probably give you a good performance benefit. The extra complexity of creating a thread pool is probably worth consideration if performance in important to you.

http://en.wikipedia.org/wiki/Thread_pool_pattern
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> I assume this memory is de-referenced

Just a note (don't accept this as answer please) : de-referencing a pointer means that you get the data that the pointer points to. It does not mean that no pointers to a given block of dynamically allocated memory exist any more (which is what you seem to understand by the term).
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> but I'm not clear whether in a threaded environment the termination of the thread implicitly frees all dynamic memory allocated by that thread
No, only when the process is terminated do resources get released back to the OS. Since all the threads run within the same process context nothing is released. You might, for example, be sharing a heap allocated object in memory between two threads. Can you imagine the trouble that would happen if the termination of the thread released this?
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> Are you saying I still need to free it explicitly even though the thread no longer exists?
I am saying, if the thread allocated it then the thread should release it. The line is a little more blurred if the heap was allocated by the main thread and then passed into the child thread. In this case it is probably better the main thread cleans up. Generally whatever allocates a resource should clean it up. There are a few exceptions, such as smart pointer (which take ownership of a resourse) but in general if you make a mess you clean it up.
Avatar of pralteb
pralteb

ASKER

Thanks for that, but I have looked into the thread pool option and don't feel that I need it for this early version of the application.  Pthread creation is very fast and my initial tests show good performance.

I really need a direct answer to my question: do threads really need to free dynamic memory explicitly or does the termination of the thread implicitly free all memory allocated by that thread?
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> In this case it is probably better the main thread cleans up
BTW: This is purely for good practice (the the thread doesn't know what else might be using this heap so to free it might be dangerous) and not for any specific technical reason. Heap allocated within a process can be free'd at any point in any thread within that process -- and at some point it must be or you'll have a leak. The decision as to where to allocate and where to free should be guided by the design you are implementing but at some point you must free or you'll just leak memory.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of sunnycoder
sunnycoder
Flag of India image

>do threads really need to free dynamic memory explicitly
Yes, an explicit free is required

>or does the termination of the thread implicitly free all memory allocated by that thread?
No, thread termination does not free up the memory that the thread allocated dynamically
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

sc, thats for repeating what I've already said :)
Avatar of pralteb
pralteb

ASKER

Thank you for your straight answer, which I shall accept.

I'm sorry if I appeared to ask the same question twice -- your answers came at such a rate that I got out of sync!
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> I'm sorry if I appeared to ask the same question twice
No worries, as long as you understand that's all that matters :)

>> your answers came at such a rate that I got out of sync!
Oops.. sorry -- that's my bad.
Avatar of sunnycoder
sunnycoder
Flag of India image

Ex,

My bad .. I should have annotated my response with an acknowledgment of what had already been posted. My response was not prompted by lack of information but by http:#22023559

sunnycoder
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

No worries sc :)

Thanks for replying.
C
C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo