LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-21-2011, 05:16 AM   #1
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Rep: Reputation: Disabled
Smile Is there some way to reduce memory cost of threads


Fedora15 32bit.

I write a test program, it creates new thread continually, the thread does nothing but sleep.

I find virtual memory increases up almost 10Mb when a new thread is created. and when there's more than 200 threads, the virtual memory used by the program is 3Gb, and now cann't create new thread.

but on windows, it costs little memory.

What can I do to config the operation system to take less memory on threads?

thanks.
 
Old 07-21-2011, 05:23 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Hi,

The trick is to keep track of your threads. Dont expect Linux to wave a magic wand and poof them away... because, well, when is a thread "not needed"???

In the "other one", it is possible to keep stuffing the thing until you overwrite something. Did that once, and got a "boot error" - and a dead os...but, those were bad times (I had to develop for the "thing)...

You need to find a way to let the main/calling system know what thread can be killed and get it to kill that thread too...

Linux works for you, it does not persé decide what has to be done, it is your system, you're not its appendix...

Thor
 
Old 07-21-2011, 05:32 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by cplus View Post
I write a test program...
It would be nice to see this test program, namely to see which programming language you are using. Also, what tool are you using to measure the amount of (virtual) memory being consumed?

If you are developing in C, then the successful completion of a thread should be followed by a call to pthread_join(). That should release any resources used by the thread.
 
Old 07-21-2011, 06:07 AM   #4
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
It would be nice to see this test program, namely to see which programming language you are using. Also, what tool are you using to measure the amount of (virtual) memory being consumed?

If you are developing in C, then the successful completion of a thread should be followed by a call to pthread_join(). That should release any resources used by the thread.

I use c++, Qt framework. use QThread to create thread.

I found that after ulimit -s 1024, it will take less memory.
 
Old 07-21-2011, 06:31 AM   #5
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
that's it is, ulimit -s 1024 set the stack size, each thread has it own stack.
 
Old 07-21-2011, 06:43 AM   #6
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by cplus View Post
that's it is, ulimit -s 1024 set the stack size, each thread has it own stack.
QThread::setStackSize(). You may also want to look at QtConcurrent and QFuture. Qt has very good documentation, so use it.
 
Old 07-21-2011, 08:13 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by cplus View Post
I use c++, Qt framework. use QThread to create thread.

I found that after ulimit -s 1024, it will take less memory.
Why not skip the Qt threads and just use pthreads? It seems like the Qt thread class is meant for writing code that isn't limited to *nix operating systems. You can also look at man 2 clone for a more "customized" version if you really want to get your hands dirty.
Kevin Barry
 
Old 07-21-2011, 10:10 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Sure... "don't create 200 threads."

Stop and consider how many hamburgers a group of about eight people can serve during a typical lunch hour, and how many hundreds of people can be placing those orders. Yet, "everything works," because the workflow (i.e. burgers) is not related to the number of workers who are doing the job.

If fast-food restaurants worked like too-many web sites do, the scenario would be like this:
  1. You walk into the store.
  2. The owner of the store pulls out a red box and pours a drop of water on it.
  3. "Just Add Water!" A new employee magically appears!!
  4. The employee takes your order, then walks into a crowded kitchen full of similar employees and starts slugging it out, trying to get your order ready.
  5. The employee hands you your burger, then drops dead on the spot.
 
Old 07-21-2011, 08:55 PM   #9
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by SigTerm View Post
QThread::setStackSize(). You may also want to look at QtConcurrent and QFuture. Qt has very good documentation, so use it.

Thanks, you let me know stacksize can be set by program it self.
 
Old 07-21-2011, 08:57 PM   #10
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
Why not skip the Qt threads and just use pthreads? It seems like the Qt thread class is meant for writing code that isn't limited to *nix operating systems. You can also look at man 2 clone for a more "customized" version if you really want to get your hands dirty.
Kevin Barry

I use QThread for the test program, in fact, our project use pthread_create.
 
Old 07-21-2011, 09:06 PM   #11
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
Sure... "don't create 200 threads."

Stop and consider how many hamburgers a group of about eight people can serve during a typical lunch hour, and how many hundreds of people can be placing those orders. Yet, "everything works," because the workflow (i.e. burgers) is not related to the number of workers who are doing the job.

If fast-food restaurants worked like too-many web sites do, the scenario would be like this:
  1. You walk into the store.
  2. The owner of the store pulls out a red box and pours a drop of water on it.
  3. "Just Add Water!" A new employee magically appears!!
  4. The employee takes your order, then walks into a crowded kitchen full of similar employees and starts slugging it out, trying to get your order ready.
  5. The employee hands you your burger, then drops dead on the spot.

thanks, sundialsvcs. your example is very lively.

our program is a realtime network program, it works well on Windows for many years, these days we migrated it to Linux, and get the problem.

We solved it by setting stack size of Linux at last. and we needn't to change our code.
 
Old 07-22-2011, 06:42 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Windows uses an entirely different thread-management protocol, and manages its affairs very differently especially with regards to memory. (Read some docs in the Wine project for a discussion of some of those.)

IMHO, your "solution" is at best a temporary workaround and management needs to know that redesign of this aspect of the application needs to become a high-priority budget item. The project does not have to be unmercifully costly, and it will reap significant performance and therefore business benefits. (The same reasoning would greatly improve the Windows application, too, if it is also still in service.)

On either system, this design is over-committing the system and doing so without adequate controls, opening the way e.g. to a denial of service. Linux/Unix is simply making you pay the price more visibly.
 
  


Reply



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
Does /dev/shm reduce memory available for non-shared memory segments? mightyscotchpine Linux - Server 1 09-22-2009 06:58 PM
LXer: Reduce your Linux memory footprint LXer Syndicated Linux News 0 02-03-2007 01:33 AM
LXer: Reduce network storage cost, complexity with ATA over Ethernet LXer Syndicated Linux News 0 07-03-2006 05:33 PM
Reduce memory to install? Put back after? What? pvdl Linux - Hardware 5 10-15-2004 12:46 AM
why using amsn cost me 300M memory preswang Linux - Software 5 11-07-2003 08:03 AM

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

All times are GMT -5. The time now is 06:39 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