Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-07-2011, 01:57 AM
|
#1
|
Member
Registered: Oct 2010
Location: Switzerland
Posts: 84
Rep:
|
[Newbie] Threads duplicating memory : normal behaviour ?
Hi everyone,
I'm currently developping a project on a SBC9261 board, with 64MB of RAM available.
I'm using two threads (from the Pthread library) in addition to the main program.
Due to recent memory leaks, I had to check how much memory the processes need (using top
) and was surprised to see that the three of them use the same amount of RAM : ~10MB.
So when they reach ~20MB (1/3 of total RAM), everything slows down and my program finally crashes.
Is this a normal behaviour ? Is there a way to make the thread share the memory, as it seems to be the same datas (same size anyway) ?
Or any other suggestions to save memory ?
Thank you for your answers !
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
03-07-2011, 08:03 AM
|
#2
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541
|
Threads can indeed share the same memory. However, you may need to protect access to this memory area using a mutex.
Here's a trivial example:
Code:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
pthread_mutex_t mutex;
typedef struct
{
int val1;
int val2;
} Data;
void* thread(void* arg)
{
Data* data = (Data*) arg;
pthread_mutex_lock(&mutex);
printf("data->val1 = %d\tdata->val2 = %d\n", data->val1, data->val2);
data->val1 = data->val2 = 1;
pthread_mutex_unlock(&mutex);
return 0;
}
int main()
{
Data* data = malloc(sizeof(Data));
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread, data);
pthread_create(&tid2, NULL, thread, data);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
free(data);
return 0;
}
|
|
|
03-07-2011, 09:39 AM
|
#3
|
Member
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600
Rep: 
|
That's a lot of memory for a thread. Since the memory model with a threaded app is a shared address space, all the threads will share all of the memory. I'm not sure what would cause so much memory to be taken, but my guess is you have some initialization routine that is misbehaving.
Measuring memory per-thread can be tricky - because all of the threads share memory, the entire "process" gets allocated X MB, and each thread may also report that it holds X MB - because each thread "does". It just so happens that they all hold the same X MB.
Hopefully this was helpful for you.
|
|
2 members found this post helpful.
|
03-08-2011, 01:47 AM
|
#4
|
Member
Registered: Oct 2010
Location: Switzerland
Posts: 84
Original Poster
Rep:
|
Quote:
Originally Posted by orgcandman
Measuring memory per-thread can be tricky - because all of the threads share memory, the entire "process" gets allocated X MB, and each thread may also report that it holds X MB - because each thread "does". It just so happens that they all hold the same X MB.
|
The reason why my threads use so much memory might be that I'm working with Qt for a GUI, and this library is quite big. Actually, the main program uses Qt, but the two other threads don't. That why I would've expected to see the main program using the 10MB, and the threads using a few ko...
The top output :
Code:
Mem: 27252K used, 34488K free, 0K shrd, 0K buff, 18224K cached
Load average: 0.29 0.07 0.02
PID USER STATUS RSS PPID %CPU %MEM COMMAND
888 root R 10176 1 8.8 16.3 MyProgram
899 root S 10176 888 0.0 16.3 MyProgram
900 root S 10176 899 0.0 16.3 MyProgram
Thank you both for your answers and don't hesitate to post if you have other suggestions !
|
|
|
All times are GMT -5. The time now is 08:28 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|