No announcement yet.

Another Rookie DLL Question

  • Filter
  • Time
  • Show
Clear All
new posts

  • Another Rookie DLL Question

    Because my app has gotten too large to compile as a single EXE,
    I'm breaking it up into multiple EXEs and DLLs. I would like
    to put a large "library" of string arrays (variables, but
    effectively used as constants by the app, since they never
    change) into a large DLL, callable by 2 or more of the EXEs.

    What I'm wondering is, if MAIN.EXE and SECOND.EXE both use the
    large DLL I want to create, and DLL is loaded into memory when
    MAIN.EXE starts, will Windows load a second copy of it it memory
    each time I SHELL to SECOND.EXE, or will SECOND.EXE simply call
    the DLL that is already in memory? Since the DLL would be quite
    large (a few hundred Kb of string arrays), this won't be a
    satisfactory solution if SECOND.EXE has to load the DLL from
    disk each time it is SHELLed to from MAIN.EXE, by the user, with
    a delay of a couple or three seconds.

    I'm hoping Windows is smart enough not to have to load multiple
    copies of a DLL into memory, but am I being too optimistic here?
    I haven't been able to find any info on this in the archives, as
    the answer is probably too obvious to old hands....


  • #2
    Try this -- Run the 1st program, shell to the 2nd program, then
    click Start > Programs > Dos Prompt. Then type MEM /C/P and see
    whats in memory.

    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.


    • #3
      I thought that was exactly the whole idea behind DLL's. That they only
      are loaded once by Windows and that multiple programs can use them 'at
      the same time'. So, to my knowledge, they are always loaded only once.
      Kind regards

      [email protected]


      • #4
        In general, the memory image of the DLL (the executable DLL code) will be loaded once into physical memory and mapped into each process that uses it. Therefore, if the memory image of the DLL is 100K, then the memory pccupied by the executable DLL code will be 100K regardless of whether it is used by 1 or 100 applications.

        Now, the memory used by the running DLL is a different subject. Each process has it's own address space, so the DLL's runtime memory requirements will be allocated separately for each Process that uses the DLL.

        For example, lets say we have two separate EXE's (Processes) that load the same DLL. Lets also say that the DLL uses 10Mb of memory for variables/arrays.

        This means that there will be one copy of the DLL in memory (but deemed to be two instances), plus 10Mb of data memory for each Process, making the total physical memory consumption 20Mb + the DLL memory image size.

        However, the 10Mb allocated to the DLL belonging to one Process is not visible to the other Process (and vice-versa). Each instance executes completely independently of the other.

        (Did I explain that clearly?! - the concepts are quite involved).

        The result is that Windows probably won't need to physically load the DLL from disk as the 2nd EXE is started (although that would take only fractions of a second anyway), however you may incur overhead if each instance (copy) of the DLL needs to initialize itself by creating some very large arrays before the DLL can be used (again, unless your code does something drastic, this should only take fractions of a second too)..

        So, your idea seems quite plausable - all that remains is for you to test the theory.

        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        mailto:[email protected]


        • #5
          If the DLL has a lot of work to do on startup, you might want to replace the "global arrays" with a a memory-mapped file which all processes can share.

          Then all you have to track in your DLL is the current subscripts/pointers or whatever you use to differentiate what the current calling copy of the program is working with.

          That is, if your DLL is really just there to serve as a big disk file of data, DLL-created arrays may not be the optimum design.

          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]


          • #6

            to follow up on what lance and michael said, don't forget to
            consider "safe threading". if what you stated about exe's
            calling exe's and dll's is the way you're heading and these
            processes have access to common variables, synchronization
            will have to be at the top of your list. lance did a good job
            of explaining this @:

            one consideration you should explore would be one small exe
            and several dll's. this might make things a bit more manageable
            from a maintenance standpoint. just a thought!!!




            • #7
              Try this -- Run the 1st program, shell to the 2nd program, then
              click Start > Programs > Dos Prompt. Then type MEM /C/P and see
              whats in memory.
              This approach will not show anything useful, since MEM will just report
              the memory arena for the current DOS box.

              Tom Hanlin
              PowerBASIC Staff


              • #8
                you can put a variable length string array in a resource file and include that in your exe
                i have just updated my previous post for pb/dll

                warning ==> the string array must be read only

                joe murphy


                [this message has been edited by joseph w murphy (edited july 26, 2001).]