Free up memory

Discussion in 'Mixed Languages' started by Alphawaves, Feb 19, 2012.

  1. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    #1 Alphawaves, Feb 19, 2012
    Last edited by a moderator: Apr 20, 2017
    Ive been using EmptyWorkingSet to free up memory in a new tool, but is this really a good/safe way of dong it (I edited slighty for all process's).:eek:

    Code:
    [DllImport("psapi.dll")] 
    // public static extern bool EmptyWorkingSet(IntPtr hProcess); 
    static extern int EmptyWorkingSet(IntPtr hwProc);
     
    public void FreeMem()
            {
                try
                {
                    // EmptyWorkingSet(Process.GetCurrentProcess().Handle); 
                    // get handle to process's
                    foreach (Process process in Process.GetProcesses())
                    {
                        try
                        {
                            // empty as much as possible of working set
                            EmptyWorkingSet(process.Handle);
                        }
                        catch
                        {
                            //...
                        }
                    }
                }
                catch (Exception Error)
                {
                    MessageBox.Show(Error.Message);
                }
            } 
    Usage = FreeMem()

    Original source: http://stackoverflow.com/questions/1812018/what-are-the-side-effects-of-using-emptyworkingset

    Any feedback ?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,103
    120
    It slowdown the system, because the enviroment clean all stream called functions in the memory, isn't recommended the use of this function on all system or in a single process, can cause errors, hangs or bugs on the cleaned process.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    #3 Alphawaves, Feb 20, 2012
    Last edited: Feb 20, 2012
    (OP)
    Thanks Josh, im a little confused with this as it does free up my system according to my function for available Ram usage ? o_O

    Edit, maybe i can redirect it to my current working process to free up.. ?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,103
    120
    #4 Josh Cell, Feb 20, 2012
    Last edited: Feb 20, 2012
    The use of EmptyWorkingSet function will "clean" the Process by handle, it will apparently UP the free memory, but can cause errors in the optimized process, certainly this applications will reload the memory with the use, here's the slowdown in the applications use.

    If you need of an large memory, I advise the use of this function on all processes, less your application, "slowdown" the system, but will make free megabytes on the memory.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    Thanks again Josh.. However it still is working ok for me here, my system is responding to every call i make for it..:eek:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,103
    120
    Yes, in faster machines, the slowdown isn't visible, it reloads faster the cleaned memory.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    Thanks Josh im aware of this problem.. :hug2:
    I think i will add with a warning!!! ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,103
    120
    :hug2:

    I've some experiences with the psapi.dll, it's present since Windows XP, some applications that clean the memory use this library.

    I see problems with this function using my Athlon 1.8GHz with 128MB of RAM :rofl6:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    Thanks Josh, well i think it best to add a warning for users that want to use this function in my tool.. :hug2:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    Does respond to a Packard Bell 2.3GHz with 256MB of RAM.. Quite well :laie:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...