LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-07-2011, 01:57 AM   #1
truboy
Member
 
Registered: Oct 2010
Location: Switzerland
Posts: 84

Rep: Reputation: 9
[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.
Old 03-07-2011, 08:03 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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;
}
 
Old 03-07-2011, 09:39 AM   #3
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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.
Old 03-08-2011, 01:47 AM   #4
truboy
Member
 
Registered: Oct 2010
Location: Switzerland
Posts: 84

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by orgcandman View Post
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 !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it normal to see many threads of df -hP running under ps -ef? threezerous Linux - Newbie 5 10-05-2009 10:05 AM
Execution threads vs normal threads jonty_11 Linux - General 2 03-26-2008 10:37 AM
is this normal rsync behaviour? nass Slackware 5 05-03-2007 01:40 PM
Is this behaviour normal? Manuel-H Linux - General 2 01-20-2005 09:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration