Announcement

Collapse
No announcement yet.

How to start program Minimized as to show less memory usage in Task Manager?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to start program Minimized as to show less memory usage in Task Manager?

    Ive written a little program in PB\DLL that sits in the background with a 10-second timer checking a file for me, and when the file changes in a particular way, it pops up a messagebox.
    All is fantastic, _BUT_ ...
    A Ctrl+Alt+Del into Task Manager shows that my program is using 1000kb of memory. If my program was visible and I was able to press the minimise button, then it would drop down to about a tenth of that, which is how I would like my program to start
    The catch is, my program doesnt have any windows, dialogs, forms etc , it just uses Msgbox
    So does anyone know how I would be able to start it in a minimised state?
    It's the last thing that I need to fix!


    ------------------
    -

    #2
    By default, PB/DLL applications are allocated a 1Mb stack frame, so are you sure the memory consumption really does drop?

    FYI, it is possible that the stack frame size may be "adjustable" in future versions of the compiler...

    Normally, when a shortcut for the app is created, you can specify the initial state that the app window should be shown, but the application itself has to honor the "nCmdState" parameter passed via WINMAIN - if the app ignores this value, then you have to find some other way to minimze the app. However, since your app does not actually contain a window/dialog, it is hard to see how you can minimize "nothing"!

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

    Comment


      #3
      cheers lance, after searching a dozen keywords with our beloved poffs i eventually found your reference to that:
      'message http://www.powerbasic.com/support/pb...ead.php?t=1870
      'forum: powerbasic for windows
      'topic: memory usage
      'name: semen matusovski, member
      'date: march 01, 2000 12:12 am

      guys --
      i found that following pb/dll program
      #compile exe
      function pbmain: msgbox "ok": end function
      takes more than 1 m of ram.
      at least, this is an information of task manager (win2000).
      meanwhile my really gigant pb/cc module takes 340 k.

      very strange ...
      '############################ follow-up ############################
      lance edmonds, administrator
      posted march 01, 2000 12:31 am

      when a pb/dll app starts, windows automatically gives the app a 1mb stack. recent discussions on this topic came to no proven conclusion, but it seems that windows may dynamically resize the stack during normal runtime operations.

      ------------------
      lance
      powerbasic support
      [email protected]
      it almost seems like i'll have to create a window and minimise it possibly... i'll play around with windows and see how it goes, will report back any findings here.
      any comments/suggestions welcome!




      ------------------
      -

      Comment


        #4
        Im using NT4 by the way, and this is based on the value that Ctrl+ALt+Del Task Manager reports in the Mem Usage column

        Code:
        #COMPILE EXE
        FUNCTION PBMAIN () AS LONG
          Msgbox "Should be using 1000kb memory"
        END FUNCTION
        ---
        Code:
        #COMPILE EXE
        #INCLUDE "win32api.inc"
        FUNCTION PBMAIN () AS LONG
          LOCAL hDlg   AS LONG
          DIALOG NEW 0, "", 0, 0, 1, 1, %WS_MINIMIZEBOX + %WS_MINIMIZE, 0 TO hDlg
          DIALOG SHOW STATE hDlg, %SW_MINIMIZE
          Msgbox "Should be using about 580kb"
        END FUNCTION
        So by adding a dialog and sending it minimised, its changing the memory usage....
        hrmmm


        ------------------
        -

        Comment


          #5
          Interesting experiment...
          Start | Run | notepad.exe ...
          Task Manager reports that it is using 1376kb
          I press Minimize, and notepad is now using 92kb
          I press the notepad icon in the tray to restore Notepad to view, and it is now using 332kb

          I tried the Address book DDT example from PB's examples directory, and it started as 1276kb (so even less than Notepad.exe)
          I clicked the Minimize button, and it then dropped down to 136kb!
          I then clicked the icon in the tray to restore Addressbook to view, and it is now just using 452kb

          So my question is how to make a program initialise in a way as to start out _under_ the 1000kb 'barrier'?


          ------------------
          -

          Comment


            #6
            The question in my mind is "is it really relevant or crucial?" - as noted before, Windows appears to resize the stack frame dynamically, so does this "excess" memory get reposessed when virtual memory gets low? ie, does it behave like 16-bit discardable resources, etc, and get deallocated (compacted?) by Windows when needed?

            Or, to put it yet another way, is the instantaneous memory usage terribly relevent in simpilistic example above?

            Given that all windows redraw themselves when restored from minimization, predicting the "true" amount of memory in use is application-dependant... predicting how Windows manages memory compacting is even more difficult!

            Out of curiousity, how does the equivalent VB and C/C++ app behave compared to NOTEPAD or your example above?

            Ranting on... signing off...


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

            Comment


              #7
              The relevance is that if somebody is reviewing my program, I want them to see the memory usage as being the least my program has to use... PB is obviously doing a very good job of it, as minimizing a PB window proves that... but thats only if the user physically minimises it themselves - id just love to know how to initialise my program so that it is running in that minimised, least-memory-used state
              As my program is a background hidden task kind of program, I should be able to run it in a 'minimised' state -
              Other NT programs that already run with minimal memory:
              smss.exe - 20k
              winlogon.exe - 136k
              notepad.exe - 96k
              spoolss.exe - 344k
              pstores.exe - 516k
              nddeagnt.exe - 40k

              So it seems there is definately a way to do this, otherwise smss.exe would surely read at around 1020kb...


              ------------------
              -

              Comment


                #8
                Add virtual memory to what task manager is presenting. When minimized,
                NT goes into hyper VM mode. Task manager's memory usage is probably meant to be a guide and not the gospel.

                ------------------
                Ron

                Comment


                  #9
                  As I understand it...

                  The "Mem Usage" column of the Windows NT Task Manager displays (basically) "how much memory is available to your program -- reserved for its use -- right now." Windows will give it more memory if it needs it, or take memory away from that total, as the demands of the system require. If your program is using memory and another program (or the OS itself) needs it, the memory will be swapped to disk. If your program is not using memory when another program needs more, your program's total will simply be reduced. But again, if your program needs more memory it will get it.

                  The memory that is used by a program is affected by the size of the stack that is allocated to it (as Lance described), the amount of memory that Windows has allocated for strings, and the number of windows that it owns, among other things.

                  When the user minimizes an application, Windows assumes that the app is becoming inactive (and therefore does not require as much memory as it did) so it re-analyzes everything and "aggressively" trims the memory usage.

                  You can affect the number that is displayed by using this code in your program...

                  Code:
                  DIM hProcess AS LOCAL DWORD
                  hProcess = OpenProcess(%PROCESS_ALL_ACCESS,0,GetCurrentProcessId)
                  SetProcessWorkingSetSize hProcess,BYVAL -1,BYVAL -1
                  CloseHandle hProcess
                  (Depending on how the parameters of SetProcessWorkingSetSize are DECLAREd (as LONGs or DWORDs)you may need to use %MAXDWORD instead of -1.)

                  WARNING: I have not measured how setting SetProcessWorkingSetSize affects an application's actual performance, and I would welcome input from others about this issue. If I understand what I've read, it should not have any effect unless your program attempts to use a large amount of memory after making the call to SetProcessWorkingSetSize, in which case it would take a small amount of time for Windows to allocate a new block of memory.

                  Also, only Windows NT and 2000 support SetProcessWorkingSetSize, so you should test the Windows version and only execute that block of code if 95/98/ME is not detected.

                  It does make a difference where in your program you place that code. If I put it at the very beginning of WinMain it has no effect. If I place it just before my program enters its message loop (i.e. after the program has used a lot of strings during init, and after all of the windows have been created) it seems to cut the displayed memory usage significantly. Some of the reading that I have done says that you should execute that code periodically, like once an hour or whenever the program goes into an "idle" or "wait for user input" mode.

                  But again, the effect (as I understand it) is more or less "cosmetic" because Windows will give your app more memory if it needs it.

                  The SetProcessWorkingSetSize function does not reduce the displayed Mem Usage nearly as far as minimizing the app does. Apparently Windows reserves its most "aggressive" memory management for apps that have been minimized by the user.

                  -- Eric


                  ------------------
                  Perfect Sync: Perfect Sync Development Tools
                  Email: mailto:[email protected][email protected]</A>

                  [This message has been edited by Eric Pearson (edited November 10, 2000).]
                  "Not my circus, not my monkeys."

                  Comment


                    #10
                    Wayne,

                    I would not lose any sleep over these memory figures, it corresponds
                    to two settings in the PE file header, "Stack Reserve" and "Stack Commit"
                    being you minimum and maximum stack usage. Default in a PowerBASIC EXE
                    is 1 meg which is more than enough in most instances. LOCAL memory in
                    a function or sub uses the stack and the demand varies with the stack
                    usage.

                    If you need more memory than is available on the stack, you are better off
                    to dynamically allocate it with GlobalAlloc(), VirtualAlloc() or one of the
                    OLE memory allocation functions. Usually stack faults come from message
                    deadlocks and other similar mistakes rahter than the stack limit size so
                    if you code is written properly, you should not have any problems.

                    Regards,

                    [email protected]

                    ------------------
                    hutch at movsd dot com
                    The MASM Forum - SLL Modules and PB Libraries

                    http://www.masm32.com/board/index.php?board=69.0

                    Comment


                      #11
                      Steve --

                      > I would not lose any sleep over these memory figures

                      I agree, but sometimes you come up against a potential customer -- usually a non-programmer -- who thinks they're an expert and beats you up about things like that. If there are "harmless" things I can do to avoid an issue, I usually do them.

                      -- Eric

                      ------------------
                      Perfect Sync: Perfect Sync Development Tools
                      Email: mailto:[email protected][email protected]</A>
                      "Not my circus, not my monkeys."

                      Comment


                        #12
                        I don't seriously beleive in "Stack" parameter.
                        At least under Win2000 I see direct link between state of first (note, first) top-level window and "memory usage".
                        [code]
                        #Compile Exe
                        #Register None
                        #Dim All
                        #Include "Win32Api.Inc"

                        Function PbMain () As Long
                        Local hDlg1 As Long, hDlg2 As Long
                        Dialog New 0, "Nothing", , , 100, 100,, %WS_EX_TOOLWINDOW To hDlg1
                        Dialog Show State hDlg1, %SW_MINIMIZE
                        Dialog New 0, "Test", , , 100, 100, %WS_SYSMENU Or %WS_CAPTION To hDlg2
                        ' Control Add Button, hDlg2, 101, "Click me", 10, 10, 80, 15
                        ' Control Add TextBox, hDlg2, 102, "", 10, 30, 80, 60
                        Dialog Show Modal hDlg2
                        End Function
                        [code]

                        For this code I see 384 K.
                        If do not create additional window - 180 K.
                        If to uncomment Control Add, memory usage increases.
                        If to begin to type - again increases.
                        If to begin to include API calls - memory increases again.

                        Note, that smss and similar programs have no windows.

                        For myself I understood that this question has no direct relation to PB compilation.
                        With "features" of OS - yes.





                        ------------------
                        E-MAIL: [email protected]

                        Comment


                          #13
                          wow! many thanks to all for the feedback, and we have a winner!
                          Semen, your code did the trick fantastically (just three lines and a declaration), my app now runs just as normal, but Task Manager now shows approx 500kb instead of 1000kb!
                          Obviously it has no huge impact on the program, but as Eric pointed out potential customers and even software reviewers often glance at such things, and whether Task Manager is gospel or not, it's shipped with NT and i think many people use it? i do
                          Anyway, im happy with that solution, it's doing the trick nicely
                          Many thanks again




                          ------------------
                          -

                          Comment


                            #14
                            I would like to add a question this this thread:

                            What relation to the memory usage discussed does the
                            operating system DLLs loaded for an app play in this ?


                            Depending on whichever API calls are used, different OS DLLs get
                            loaded by an app. The memory usage reported, is it affected by
                            the OS DLLs loaded by the app.


                            ------------------
                            Chris Boss
                            Computer Workshop
                            Developer of "EZGUI"
                            http://cwsof.com
                            http://twitter.com/EZGUIProGuy

                            Comment


                              #15
                              Wayne --

                              > Semen, your code did the trick fantastically

                              I think you meant my code, but in any case...

                              More info... When you perform the SetProcessWorkingSetSize operation periodically, you will probably see even further reductions. My program calls my SetProcessWorkingSetSize function just before it enters its main message loop, and again every 10 minutes on a timer (but only if the program has been idle). I see an initial reduction from around 1500k to around 6-800, then down to around 350. But if I call the function twice in a row just before entering the message loop, there is no additional gain. I have no explanation for that.

                              So calling it every so often appears to help. On the other hand, calling the function too often will probably result in a lot of "memory thrashing" and a performance penalty.

                              Chris --

                              I'm not sure I understand your question.

                              > The memory usage reported, is it affected by
                              > the OS DLLs loaded by the app.

                              Processes use memory, not modules. But sure, if your application uses a DLL (system or otherwise) that uses a lot of memory, I would expect the NT task manager to show that usage. But DLLs don't have stacks, and I believe they get their string memory from the same pool that is allocated to the process (the EXE), so I would not expect the use of a DLL to increase the "reserved" memory that a program has, just the "used" memory, if any.

                              -- Eric


                              ------------------
                              Perfect Sync: Perfect Sync Development Tools
                              Email: mailto:[email protected][email protected]</A>
                              "Not my circus, not my monkeys."

                              Comment


                                #16
                                Originally posted by Eric Pearson:
                                I think you meant my code
                                Eric --
                                I'm doubt that Wayne made a mistake.
                                At least, under Win2000 there is an effect, which is not possible to explan.

                                Wayne -
                                I'm lazy to reboot - did you test under 9x ?
                                (not sure that such effect exists here)

                                ------------------
                                E-MAIL: [email protected]

                                Comment


                                  #17
                                  You have to remember that EXE's are only mapped to linear address space when loaded.
                                  Parts are swapped into physical memory whenever they're accessed.

                                  I think this playes a major role in what y'all are seeing.


                                  Peter.


                                  ------------------
                                  [email protected]

                                  Comment


                                    #18
                                    Eric, I did mean Semen's code!
                                    I haven't tried your code yet as it isn't for Win9x, however you may be onto something with that API call - would you be kind enough as to make a little hello world example for me?
                                    Is it more beneficial than Semen's example? (which works on my mothers Win98 box, although I dont have any programs to measure the memory usage at the moment)




                                    ------------------
                                    -

                                    Comment


                                      #19
                                      > I did mean Semen's code!

                                      My apologies to both you and Semen... When you said "three lines of code and a declaration" I thought you meant my code, because Semen's appeared to be much longer. I see now that the "solution" part of Semen's code is that brief.

                                      > would you be kind enough as to make
                                      > a little hello world example for me?

                                      Sure, I'll put something together and post it later today. But it will have no effect on Windows 9x systems, since the API is only supported by NT/2000.

                                      > which works on my mothers Win98 box

                                      Semen's technique is providing good results on my NT machine too.

                                      > I dont have any programs to measure the
                                      > memory usage at the moment

                                      Have you tried Start / Programs / Accessories / System Tools / System Monitor?

                                      -- Eric


                                      ------------------
                                      Perfect Sync: Perfect Sync Development Tools
                                      Email: mailto:[email protected][email protected]</A>
                                      "Not my circus, not my monkeys."

                                      Comment


                                        #20
                                        Semen --

                                        Does your technique work on Windows 98 systems? I have been using the 98 System Monitor to check, and I'm not seeing any effect.

                                        Wayne --

                                        > Is [the API technique] more beneficial than Semen's example?

                                        No, my initial testing indicates that Semen's technique appears to give slightly better results. I'm a little concerned about "using up" the first-created window because it has other special properties that I need in my program -- I need to make it visible -- but I'm going to investigate this technique further...

                                        -- Eric

                                        ------------------
                                        Perfect Sync: Perfect Sync Development Tools
                                        Email: mailto:[email protected][email protected]</A>

                                        [This message has been edited by Eric Pearson (edited November 10, 2000).]
                                        "Not my circus, not my monkeys."

                                        Comment

                                        Working...
                                        X
                                        😀
                                        🥰
                                        🤢
                                        😎
                                        😡
                                        👍
                                        👎