Results 1 to 11 of 11

Thread: Reduce the memory usage.

  1. #1

    Thread Starter
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    Reduce the memory usage.

    Ok, there has been a number of complaints about programs written in the .NET Framework using lots of memory. Here is a way to reduce it using P-Invoke.

    You need to import the function SetProcessWorkingSetSize.

    VB Code:
    1. <DllImport("kernel32.dll")> _
    2. Public Shared Function SetProcessWorkingSetSize(ByVal handle As IntPtr, _
    3.         ByVal min As Int32, _
    4.         ByVal max As Int32) _
    5.         As Boolean
    6. End Function
    Now call the function. You can place this in the constructor.
    VB Code:
    1. Dim proc As Process = Process.GetCurrentProcess()
    2. Me.SetProcessWorkingSetSize(proc.Handle, -1, -1)

    C# Version
    Code:
    [DllImport("kernel32.dll")]
    public static extern bool SetProcessWorkingSetSize(System.IntPtr handle, int min, int max);
    Code:
    Process proc = Process.GetCurrentProcess();
    SetProcessWorkingSetSize(proc.Handle, -1, -1);
    Also, make sure you have a reference to System.Diagnostics and System.Runtime.InteropServices.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Why not using MaxWorkingSet of the Process?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Well, I never thought of that but I'll try.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is there a remarkable difference while using this function ?

  5. #5

    Thread Starter
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I tried it and it reduced a program from 15 mb to 4 mb.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Great . I'll try it too soon . Thanks

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I doubt that this function be useful in oridinary situations. If you have enugh memory, then why not let the program expand itself? I think the .NET memory manager is smart enough to shrink memory usgae when there is not enugh memory, that's just a guess though.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tell that to ur crazy clients when they are used to 10mb progs and they see 30mb ones
    \m/\m/

  9. #9
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Lunatic3
    I doubt that this function be useful in oridinary situations. If you have enugh memory, then why not let the program expand itself? I think the .NET memory manager is smart enough to shrink memory usgae when there is not enugh memory, that's just a guess though.
    I wouldn't call garbage collection "smart"
    Last edited by Kasracer; Sep 1st, 2003 at 01:26 AM.

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Originally posted by kasracer
    I would call garbage collection "smart"
    yeah
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  11. #11
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Lunatic3
    yeah
    *wouldn't

    typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width