Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Easy memory optimizations?

Easy memory optimizations?

Scheduled Pinned Locked Moved C#
performancedelphialgorithmsquestioncareer
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bbranded
    wrote on last edited by
    #1

    Hello, I just finished one of my first programs, a program to show and hide windows by process. I'd like to go ahead and give it to my boss to put it in production, but the memory usage ranges from 12-15MB, and doesn't seem to make any sense. A co-worker of mine has a similar piece of software (handles z-order of windows), which he programmed in delphi, and it only takes 4-5MB of RAM. I attempted to call the garbage collector more frequently, but, as expected, this didn't have much of an effect (from what I've been reading, it does its job pretty efficiently on it's own). Does anyone have any tips for memory optimization? Thanks very much, Matt

    D L D 3 Replies Last reply
    0
    • B bbranded

      Hello, I just finished one of my first programs, a program to show and hide windows by process. I'd like to go ahead and give it to my boss to put it in production, but the memory usage ranges from 12-15MB, and doesn't seem to make any sense. A co-worker of mine has a similar piece of software (handles z-order of windows), which he programmed in delphi, and it only takes 4-5MB of RAM. I attempted to call the garbage collector more frequently, but, as expected, this didn't have much of an effect (from what I've been reading, it does its job pretty efficiently on it's own). Does anyone have any tips for memory optimization? Thanks very much, Matt

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      First, if you're looking in TaskManager to see the memory usage, it's lying to you. You're app isn't taking that much memory. A .NET app, like Java, runs in a kind of virtual machine. What you're seeing is what the .NET CLR has reserved for your application, even if the memory isn't being used. No, you can't do anything about it since all memory management is done automatically by the CLR. If Windows needs the memory the CLR has reserved back, the CLR is more than happy to return whatever it can. The reseason the memory is reserved to start with is because when your code allocates memory for an object, it's much quickler to allocate it out of the managed heap the CLR maintains, than it is to go back to Windows, ask for a block of memory, add it to the managed heap, then allocate your object. Calling the GarbageCollector doesn't do anything for you except screw up the internal optimization algorithm it uses. Unless you're an advanced developer with a definite need to do this and know precisely why you're doing it and the consequences of doing it, you show never force the GC to do an early collection. Plus, 12 to 15MB of memory is nothing now-a-days, so what are you worried about??

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      Y 1 Reply Last reply
      0
      • B bbranded

        Hello, I just finished one of my first programs, a program to show and hide windows by process. I'd like to go ahead and give it to my boss to put it in production, but the memory usage ranges from 12-15MB, and doesn't seem to make any sense. A co-worker of mine has a similar piece of software (handles z-order of windows), which he programmed in delphi, and it only takes 4-5MB of RAM. I attempted to call the garbage collector more frequently, but, as expected, this didn't have much of an effect (from what I've been reading, it does its job pretty efficiently on it's own). Does anyone have any tips for memory optimization? Thanks very much, Matt

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, I agree with Dave.

        bbranded wrote:

        Does anyone have any tips for memory optimization?

        The best optimization tip, assuming the functionality requirements warrant it: add lots of code and functionality, and rejoice when multiplying the source by 1000 makes the memory footprint grow by a much smaller factor. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        modified on Sunday, June 12, 2011 9:03 AM

        B 1 Reply Last reply
        0
        • D Dave Kreskowiak

          First, if you're looking in TaskManager to see the memory usage, it's lying to you. You're app isn't taking that much memory. A .NET app, like Java, runs in a kind of virtual machine. What you're seeing is what the .NET CLR has reserved for your application, even if the memory isn't being used. No, you can't do anything about it since all memory management is done automatically by the CLR. If Windows needs the memory the CLR has reserved back, the CLR is more than happy to return whatever it can. The reseason the memory is reserved to start with is because when your code allocates memory for an object, it's much quickler to allocate it out of the managed heap the CLR maintains, than it is to go back to Windows, ask for a block of memory, add it to the managed heap, then allocate your object. Calling the GarbageCollector doesn't do anything for you except screw up the internal optimization algorithm it uses. Unless you're an advanced developer with a definite need to do this and know precisely why you're doing it and the consequences of doing it, you show never force the GC to do an early collection. Plus, 12 to 15MB of memory is nothing now-a-days, so what are you worried about??

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          Y Offline
          Y Offline
          Yusuf
          wrote on last edited by
          #4

          Dave Kreskowiak wrote:

          Plus, 12 to 15MB of memory is nothing now-a-days, so what are you worried about??

          from OP

          OP wrote:

          A co-worker of mine has a similar piece of software (handles z-order of windows), which he programmed in delphi, and it only takes 4-5MB of RAM.

          The Boss will make big fuss about .net :-O

          Yusuf

          D 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, I agree with Dave.

            bbranded wrote:

            Does anyone have any tips for memory optimization?

            The best optimization tip, assuming the functionality requirements warrant it: add lots of code and functionality, and rejoice when multiplying the source by 1000 makes the memory footprint grow by a much smaller factor. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            modified on Sunday, June 12, 2011 9:03 AM

            B Offline
            B Offline
            bbranded
            wrote on last edited by
            #5

            Thanks for the info guys. I remember reading a blog post about it, but failed to be able to cite it to the senior dev. I understand what you're laying Luc. Neato. Thanks, Matt

            L 1 Reply Last reply
            0
            • B bbranded

              Thanks for the info guys. I remember reading a blog post about it, but failed to be able to cite it to the senior dev. I understand what you're laying Luc. Neato. Thanks, Matt

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              you're welcome. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              1 Reply Last reply
              0
              • B bbranded

                Hello, I just finished one of my first programs, a program to show and hide windows by process. I'd like to go ahead and give it to my boss to put it in production, but the memory usage ranges from 12-15MB, and doesn't seem to make any sense. A co-worker of mine has a similar piece of software (handles z-order of windows), which he programmed in delphi, and it only takes 4-5MB of RAM. I attempted to call the garbage collector more frequently, but, as expected, this didn't have much of an effect (from what I've been reading, it does its job pretty efficiently on it's own). Does anyone have any tips for memory optimization? Thanks very much, Matt

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                This isn't a solution as such - but an observation. When an .Net application is minimized, the reported RAM useage in task manager decreases dramtically. I found this with an app that I did that ran in the tray mostly. On launch I forced it to minimize before placing it in the tray and all the clients stopped moaning! Also, make sure you've compiled it as 'Release', not debug if you're just testing/running the exe rather than an install, that can make a difference too.

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                B 1 Reply Last reply
                0
                • D DaveyM69

                  This isn't a solution as such - but an observation. When an .Net application is minimized, the reported RAM useage in task manager decreases dramtically. I found this with an app that I did that ran in the tray mostly. On launch I forced it to minimize before placing it in the tray and all the clients stopped moaning! Also, make sure you've compiled it as 'Release', not debug if you're just testing/running the exe rather than an install, that can make a difference too.

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  B Offline
                  B Offline
                  bbranded
                  wrote on last edited by
                  #8

                  Thanks Dave. I'll see if I can work something out with minimize. I was looking for this today, is there a way to specify this in C# Express? Thanks! Matt [edit] Found it! http://cygon.nuclex.org/2006/07/02/debug-and-release-in-visual-c-express/[^]

                  modified on Monday, March 2, 2009 7:11 PM

                  1 Reply Last reply
                  0
                  • Y Yusuf

                    Dave Kreskowiak wrote:

                    Plus, 12 to 15MB of memory is nothing now-a-days, so what are you worried about??

                    from OP

                    OP wrote:

                    A co-worker of mine has a similar piece of software (handles z-order of windows), which he programmed in delphi, and it only takes 4-5MB of RAM.

                    The Boss will make big fuss about .net :-O

                    Yusuf

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Yusuf wrote:

                    The Boss will make big fuss about .net

                    Then the boss needs to read up on it and make form and educated opinion. The size of the runtime means nothing when it's productivity in writing code that's far more important. Ask him if he knows how memory management works in the .NET CLR. Does he know how it works in Delphi? Then compare the two. Oh, and this reading is not just for the boss. It should be for you too, so you can explain it to him...

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups