none
Preview of heap allocation RRS feed

  • Question

  • Hello!
     Iam new in C# programming. Currently I had developed some small app which utilizes some basic functions. It consist of initializing Form and some controls. But ... It takes ~30MB in memory (RAM). On the other side the same app based on WxWidgets and C++ it takes ... 4MB of RAM. Trying to optimize this process I have read some whitepapers on GC etc. But still Iam getting no results on reducing the ram consumption. 

    I would use some tool or technique to preview allocated ram on heap (the ints, strings, other declared objects such as my own objects or structs). Is there a way to achieve this ?

    It would help me to make assesment which of objects wasn't "freed" and still "in place" after app meet some of conditions.

    Sincerely
    -- Cris
    Sunday, September 7, 2008 3:52 PM

Answers

  • Trying to save 4 bytes of memory is totally counter-productive using a .NET language.  If that is an important parameter in your coding, you should code native C++ apps.
    • Proposed as answer by curt at amotif Tuesday, September 9, 2008 12:37 AM
    • Marked as answer by Zhi-Xin Ye Tuesday, September 9, 2008 11:41 AM
    Sunday, September 7, 2008 8:07 PM
  • Yeah, you're on the wrong track with this.  For one, local variables of a value types like "somevar" are stored on the stack, not the heap, just like they were in a C/C++ program.  Assigning null to an object is a waste of time, the garbage collector is smart enough to track the life-time of a local variable within a function.  The JIT compiler will simply generate no code for the statement in the release build.

    You'll also need to stop fretting about a program using less than 3% of the typical amount of RAM available in a reasonably modern PC.  Developing GUI apps in C++ was reasonable 10 years ago.  The leap-year of horse power available in machines today is not just for playing games, it is also there to help you be productive developing apps.  Once you've written some apps in managed code, you'll never look back.  Don't sweat the small stuff.

    Hans Passant.
    • Proposed as answer by curt at amotif Tuesday, September 9, 2008 12:37 AM
    • Marked as answer by Zhi-Xin Ye Tuesday, September 9, 2008 11:40 AM
    Sunday, September 7, 2008 8:59 PM
    Moderator

All replies

  •  One of the major attributes of managed code is the management of resources by the framework.  Unless the memory used by your app is increasing without bound, there is little you can or should do to control memory usage.  30MB is typical for a small .NET app to show in Task Manager.
    Sunday, September 7, 2008 5:49 PM
  •  Iam aware of that. Thats why also Iam asking about preview of such memory heap.  And about memory managing itself. I stumbled on a whitepaper describing that:

    int somevar = 10

    will be holded thru whole program execution var at some of 4bits of memory. While:
    int somevar = 10;  
    //some other operations in app  
    somevar = null;  
    //other operations 

     will be collected by the GC after assigning null to it. Preview of heap would allow me to "sweep" such vars and objects so the memory usage could be "cut".
    Sunday, September 7, 2008 6:55 PM
  • Trying to save 4 bytes of memory is totally counter-productive using a .NET language.  If that is an important parameter in your coding, you should code native C++ apps.
    • Proposed as answer by curt at amotif Tuesday, September 9, 2008 12:37 AM
    • Marked as answer by Zhi-Xin Ye Tuesday, September 9, 2008 11:41 AM
    Sunday, September 7, 2008 8:07 PM
  • Yeah, you're on the wrong track with this.  For one, local variables of a value types like "somevar" are stored on the stack, not the heap, just like they were in a C/C++ program.  Assigning null to an object is a waste of time, the garbage collector is smart enough to track the life-time of a local variable within a function.  The JIT compiler will simply generate no code for the statement in the release build.

    You'll also need to stop fretting about a program using less than 3% of the typical amount of RAM available in a reasonably modern PC.  Developing GUI apps in C++ was reasonable 10 years ago.  The leap-year of horse power available in machines today is not just for playing games, it is also there to help you be productive developing apps.  Once you've written some apps in managed code, you'll never look back.  Don't sweat the small stuff.

    Hans Passant.
    • Proposed as answer by curt at amotif Tuesday, September 9, 2008 12:37 AM
    • Marked as answer by Zhi-Xin Ye Tuesday, September 9, 2008 11:40 AM
    Sunday, September 7, 2008 8:59 PM
    Moderator
  • Well you guys probably right that .NET enviroment wasn't "prepared" to write apps with a bigger demand of memory control from programmer side. The app which make me to post here is a resident aplication (trayed in). Probably the best way for me would be coding the GUI in .NET (as it's far easier) and resident module in WxWidgets or MFC which will trigger parametrized .NET app.  I really can't allow that resident app (as a matter a Form app which was minimized to tray) will take ... 100MB after few hours, for a really simple app.

    Thx for your time and answers guys.
    Sunday, September 7, 2008 10:33 PM