Jump to content

_ReduceMemory UDF


jftuga
 Share

Recommended Posts

Hmm, I just realized this function requires the psapi.dll. That's the one that doesn't come with WinNT4 right?

So you might want to mention that in the function or something. Maybe do a fileexist for psapi.dll.

If @OSVersion = 'WIN_NT4' Then FileInstall('psapi.dll', @SystemDir & '\psapi.dll')

How's that look for making sure the psapi is on the system?

Link to comment
Share on other sites

@w0uter:

Shouldn't this code do the exact same thing that yours does?

Func _ReduceMemory($iPID = -1)
    Local $aHandle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1F0FFF, 'int', False, 'int', $iPID), $aReturn = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $aHandle[0])
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $aHandle[0])
    Return $aReturn[0]
EndFunc  ;==>_ReduceMemory()

Plus it's slightly faster than your function, about 0.3 ms (on my comp) averaged 50 times for each function. Here is the time trial:

Func _ReduceMemory1($iPID = -1)
    Local $aHandle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1F0FFF, 'int', False, 'int', $iPID), $aReturn = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $aHandle[0])
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $aHandle[0])
    Return $aReturn[0]
EndFunc  ;==>_ReduceMemory()

Func _ReduceMemory2($i_PID = -1)
    
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    
    Return $ai_Return[0]
EndFunc;==> _ReduceMemory()

$end = 0
For $i = 1 to 50
    $pid = Run("notepad.exe")
    ProcessWait($pid)

    $start = TimerInit()
    _ReduceMemory1($pid)
    $end = $end + TimerDiff($start)

    ProcessClose($pid)

    Sleep(250)
Next
$time1 = $end / 50

$end = 0
For $i = 1 to 50
    $pid = Run("notepad.exe")
    ProcessWait($pid)

    $start = TimerInit()
    _ReduceMemory2($pid)
    $end = $end + TimerDiff($start)

    ProcessClose($pid)

    Sleep(250)
Next
$time2 = $end / 50

MsgBox(0, "Average Speed", "_ReduceMemory1() = " & $time1 & @CRLF & "_ReduceMemory2() = " & $time2)

It's not a whole lot of speed improvement but I am always glad to help out! :)

Edited by erifash
Link to comment
Share on other sites

_ReduceMemory1() = 1.45895879641148

_ReduceMemory2() = 1.39595427417348

also you func fails if it tries to operate on itself with the -1 pid.

<{POST_SNAPBACK}>

I modify this code but I can't see any memory usage reduced. Is there anything wrong?

Func _ReduceMemory1($iPID = -1)

Local $aHandle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1F0FFF, 'int', False, 'int', $iPID), $aReturn = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $aHandle[0])

DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $aHandle[0])

Return $aReturn[0]

EndFunc ;==>_ReduceMemory()

Func _ReduceMemory2($i_PID = -1)

If $i_PID <> -1 Then

Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)

Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])

DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])

Else

Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)

EndIf

Return $ai_Return[0]

EndFunc;==> _ReduceMemory()

Local $newlist,$oldlist

while 1

$list = ProcessList()

$newlist=$list[0][0]

If $oldlist<>$newlist Then

for $i = 1 to $list[0][0]

$pid=$list[$i][1]

_reducememory1($pid)

_reducememory2($pid)

next

$oldlist=$newlist

EndIf

sleep (5000)

WEnd

Link to comment
Share on other sites

  • 2 years later...

Hello.

Im using a HP Omnibook 6100 :S only got 128 ram. so if i open to many webpages or if more than 2 person pms on msn then the computer gets really slow :S

Can any of u write whole the code and post it here?

(i use Win XP)

Link to comment
Share on other sites

  • 9 months later...
  • 2 months later...

I'm trying to use this on all processes running on a computer... is there any better way to do it than this?

Func _ReduceMemory()
    $list = ProcessList()
    For $i = 1 To $list[0][0]
        Local $ai_Handle = DllCall("kernel32.dll", "int", "OpenProcess", "int", 0x1f0fff, "int", False, "int", $list[$i][1])
        Local $ai_Return = DllCall("psapi.dll", "int", "EmptyWorkingSet", "int", $ai_Handle[0])
        DllCall("kernel32.dll", "int", "CloseHandle", "int", $ai_Handle[0])
    Next
EndFunc ;==>_ReduceMemory
Edited by mblunk
Link to comment
Share on other sites

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 8 months later...

when i first saw the memory reduction UDF earlier somewhere, i had started this simple udf to get the list of all processes and freeing up the memory - light duty - windows way - harmless. coz some memory optimisers force freeing of memory and at times, i get trouble of frozen processes for few moments, as the cache in RAM is cleared and firefox with many tabs, etc take longer time to respond after running the RAM optimiser. i am actully gonna work when i get free time to create a script to check memory level and when memory goes down , it will automatically trigger auto free if it is still low, then we will call for a third party ram freeing application :-) something like Memory fusion already in here

Edited by rajeshontheweb
Link to comment
Share on other sites

  • 1 month later...

a bit late post, but just my minor mod for newbies who wanna use it

Func _ReduceMemory($ProcID = 0)
; Original version : w_Outer 
; modified by Rajesh V R to include process ID 
    If $ProcID = 0 or ProcessExists($ProcID) = 0 Then ; No process id specified or process doesnt exist - use current process instead.
        Local $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId')
        $ProcID = $ai_GetCurrentProcessId[0]
    EndIf
    
    Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ProcID)
    Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Return $ai_Return[0]

EndFunc

this will get your process id as input if u dont supply a input then it will automatically reduce current process's memory. i am thinking of checking if it is a pid or a string and supply string name as well, but i will see if i happened to need that mod i will post it here

<edit> oops! small error in variable name...

Edited by rajeshontheweb
Link to comment
Share on other sites

it is better to use GetCurrentProcess for the own Process and not OpenProcess :)

Func _ReduceMemory($ProcID = 0)
; Original version : w_Outer
; modified by Rajesh V R to include process ID
; modified by Prog@ndy to include process handle
    
    If $ProcID = 0 or ProcessExists($ProcID) = 0 Then ; No process id specified or process doesnt exist - use current process instead.
        Local $ai_GetCurrentProcess = DllCall('kernel32.dll', 'ptr', 'GetCurrentProcess')
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', $ai_GetCurrentProcess[0])
        Return $ai_Return[0]
    EndIf
   
    Local $ai_Handle = DllCall("kernel32.dll", 'ptr', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ProcID)
    Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', $ai_Handle[0])
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
    Return $ai_Return[0]

EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...