develooper Front page | perl.ithreads | Postings from June 2008

Re: Memory usage

Thread Previous | Thread Next
From:
Jerry D. Hedden
Date:
June 11, 2008 06:32
Subject:
Re: Memory usage
Message ID:
1ff86f510806110632r5dcb9e98vafb9e4e82ae58fd5@mail.gmail.com
On Wed, Jun 11, 2008 at 9:26 AM, Javier Terceiro <correolista@gmail.com> wrote:
> Hello list,
>
> I am designed a new software. I am using threads. The operation is
> correct, but memory consumption is excessive. The system is using 100
> MB and when the software is working, 500 Mb. The memory is not
> released, why?

You need to either join the threads after they're done working, or
else detach them.  See below.

>
> The source code is:
>
> -----------------------------------------------------------------------------
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
> use AnyData;
> use Sys::Syslog;
> use Sys::Syslog qw(:DEFAULT setlogsock);
> use Sys::Syslog qw(:standard :macros);
> use POSIX qw(setsid);
> use threads;
> use threads::shared;
>
> sub mail
> {
>        print ("The mail thread\n");
>        return 0;
> }
>
> sub mf
> {
>        print ("The mf thread\n");
>        return 0;
> }
>
> sub scan_files
> {
>        while(1)
>        {
>                foreach (readdir(DIRHANDLE))
>                {
>                        if (!-d $_)
>                        {
>                                my $opt =
> choose_file_option("workq/",$_,"Delivered");
>                                switch ($opt)
>                                {
>                                        case "mail"
>                                        {
>                                                threads->new(\&mail, $_);

threads->new(\&main, $_)->detach();

>                                        }
>                                        case "mf"
>                                        {
>                                                threads->new(\&mf, $_);

threads->new(\&mf, $_)->detach();

>                                        }
>                                        else
>                                        {
>                                                syslog ('daemon|info',
> "Send mail. Failure detected");
>                                        }
>                                }
>                        }
>                }
>                sleep(60);
>        }
> }
>
> sub daemon
> {
>        chdir "/";
>        open STDIN, '/dev/null';
>        open STDOUT, '>/dev/null';
>        open STDERR, '>/dev/null';
>        fork && exit;
>        setsid();
>        if (open PID, '>', '/var/run/program.pid')
>        {
>                print PID "$$\n";
>                close PID;
>        }
> }
>
> setlogmask( LOG_MASK($Fax::Config::param{'log_info'}) );
> openlog 'faxqr', 'pid', LOG_DAEMON;
> daemon();
>
> $SIG{"TERM"} = \&notify_exit;
>
> scan_files();
>
> -----------------------------------------------------------------------------
>
> Any idea that there is a very high consumption of memory.
>
> --
> A greeting,
>
> Javier.
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About