Why is C# memory hog?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    Why is C# memory hog?

    So I have a Base C# Windows App... I've changed a few basic options on the Form, BGcolor, Title, Window Type...
    No code.
    Its 9.5 MB in memory...

    Forgive me, but what is that all about?
    And is there ways I can not have a C# program so bloated like that?!
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I believe it's just the regular .NET settings, and the garbage collector that runs in the background to clean up all your stuff.

    I did a google search for "C# memory footprint" and a discussion on bytes turned up. Hopefully it helps you out :)

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      Code:
      public class MemoryManagement {
      	[DllImport("kernel32.dll")]
      	public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
      	public void FlushMemory() {
      		GC.Collect();
      		GC.WaitForPendingFinalizers();
      		if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
      		SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
      } } }
      Error: The type or namespace name 'DllImportAttri bute' could not be found (are you missing a using directive or an assembly reference?)

      Didn't even make it to the execution portion. Do I need a reference?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I believe you need a...

        Code:
        using System.Runtime.InteropServices;
        ... at the top. Google for the win :)

        Oh hey, just a heads up, calling GC.Collect manually is supposedly not advised unless you really need it. Here's an article to read: http://blogs.msdn.com/ricom/archive/...29/271829.aspx (Again, Google :D)

        Is there any particular reason you need to do this? The only real reason I could think of is if you're running on a limited memory platform and are hitting the memory ceiling with your application. Nine times out of ten, you can resolve this by handling your memory management a bit better (allocate data in "pages" instead of one gigantic block, perhaps). The Garbage Collector will free up memory when there is a demand for it, it just doesn't run constantly to save processor power.

        I hope this information helps you!

        Comment

        Working...