mbr has asked for the wisdom of the Perl Monks concerning the following question:

Suppose that a perl program has need of only one exported symbol from a particular module. Then it makes sense to do 'use <module> "symbol"', instead of including the whole module with 'use <module> yes?. My real question is whether or not the perl process will consume less memory on a machine by doing 'use <module> "symbol"' instead of including the whole thing?

In an effort to test this, I ran the following two commands on my (Linux) system and looked at the memory utilization for each process in the process table they appeared to be the same: $ perl -e 'use Socket "AF_INET"; sleep 10;' & and $ perl -e 'use Socket: sleep 10;' & (This is my first post, so "hello")..

--Mike

Edit: chipmunk 2001-09-16

Replies are listed 'Best First'.
Re: modules and memory usage
by blakem (Monsignor) on Sep 17, 2001 at 06:35 UTC
    Unless the module takes some rather tricky steps to enable this, you won't save anything that way. See CGI.pm for an example of a module that takes extrordinary pains to allow for a miminal 'use'.

    When you provide a list of symbols, you are simply asking that they be exported (read aliased) into the current name space. See the use documentation to clarify why this is so, specifically the difference between use Module LIST and use Module ().

    -Blake

      Somewhat of a nit: the many forms of AUTOLOADing end up saving memory when you fail to call most of the routines that a module provides. Importing the routine names takes up extremely little memory and doesn't trigger the AUTOLOADing of the routines.

      So you could import hundreds of routines from a module that uses AUTOLOADing and see almost no increase in memory usage so long as you never actually called any of those routines. Conversely, you could import none of them and call lots of them (either directly or indirectly) and see a big increase.

      I routine only import the routines that I use, but my reasons have nothing to do with memory consumption. A small part of it is that I'm against namespace pollution. A bigger part of it is that listing the routines that I use on the use line is a very handy form of documenting where you need to go to figure out what WhingeGromets() is doing when you see it later in the code. It is a nice form of functional documentation because if it gets out-of-date, it automatically tells you so (well, except that Perl never complains if you import a routine and then never use it).

              - tye (but my friends call me "Tye")
Re: modules and memory usage
by Flame (Deacon) on Sep 17, 2001 at 06:11 UTC
    I don't claim to know a lot about the internals of module behavior, but I believe that this is what happens:
    Perl loads the module first
    Scans for any other commands you wanted executed on it. IE: Version check... or exported commands
    Executes the commands

    The thing is, even if it doesn't export the subroutines, it still has them all loaded into memory usually, under the package's namespace. IE: MyPackage::non_exported_sub() would still call it under most circumstances. There are modules designed to only load the subs that you ask for, but most are not.

    I hope this helps


    "Wierd things happen, get used to it"

    Flame ~ Lead Programmer: GMS
    http://gms.uoe.org
Re: modules and memory usage
by dragonchild (Archbishop) on Sep 17, 2001 at 18:18 UTC
    *sighs* You're not using Perl for its amazingly small footprint on your RAM or your CPU. You're using Perl for its amazingly small footprint on your development/maintenance time.

    In other words, if you're having to worry about if this module will take an additional 1k of RAM, you need to either:

    1. Buy more RAM and stop worrying about it
    2. Use another language and stop worrying about it
    This may sound a little harsh, but think about it.

    Oh - welcome aboard! :)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: modules and memory usage
by perrin (Chancellor) on Sep 17, 2001 at 17:48 UTC
    If you really care about memory consumption, the best thing to do is not import any symbols at all from the module by passing an empty list: use Socket ();

    Then you can just access functions by using fully-qualified names. Any symbol you import will take up more memory than this.