Jump to content
Sign in to follow this  
kiboost

ram leak with little cmd loop

Recommended Posts

kiboost

Hi,

Here is a script I use to list all running process in a txt file, every 5sec, which is then read by an intranet server :

AutoItSetOption (Opt("TrayIconHide", 1) )

$kname = @ComputerName

$file_path = "\\demeter\Documents\_RenderfarmCache\" & $kname & ".txt"
$command = StringFormat('pslist.exe -accepteula %s > "%s"',"",$file_path)

$i=0
While $i <= 10
    Run(@ComSpec & ' /C ' & $command, @ScriptDir, @SW_HIDE)
    Sleep(5000) ;five seconds
WEnd

All runs fine, BUT it eat my ram ! This is a few ko each time so I didn't see it previously, but after a few hours it take lot of ram (not seen in process, but in ram avaliable). I have tested on a fresh boot xp, with and without it running. Maybe each cmd run stay in ram ? How could I free up ram after each loop to not eat ram like that ?

Thanks

Kib


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites
kiboost

Kib,

What's the difference of using your CMD switch over ProcessList()

James

Hi James,

I saw breifly processlist, but didn't found a way to male it writing in a txt file. I need a list (in any way) of running process in a txt file, do do a reg_match in php on the intranet server.

I would indeed prefer using processlist, if you can enlighten me I would really appreciate.


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites
kiboost

Seems you pointed me in the right direction,

Here I am :

$list = ProcessList()
$Return = $kname
for $i = 1 to $list[0][0]
    $Return = $Return & @CRLF & $list[$i][0]
next
msgbox(0, "bibi", $Return)

Now have to put the msgbox inside the txt file, and put all this in function every 5sec.

Which loop method would you use to be sure nothing eat ram like previous "version" ?


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites
Mat

While 1
    ; Loop
    Sleep(5000)
WEnd

You were making an infinite loop doing much more complex stuff which can't have been helping. If you get high mem usage then you might want to use the EmptyWorkingSet function.

Share this post


Link to post
Share on other sites
kiboost

Thanks Mat,

Here is my script, finally :

AutoItSetOption (Opt("TrayIconHide", 1) )

$kname = @ComputerName

$file_path = "\\demeter\Documents\_RenderfarmCache\" & $kname & ".txt"

While 1
    $file = FileOpen($file_path, 2)

    $list = ProcessList()
    $Return = $kname

    for $i = 1 to $list[0][0]
        $Return = $Return & @CRLF & $list[$i][0]
    next

    FileWriteLine($file, $Return)
    FileFlush($file)
    FileClose($file)
    
    Sleep(5000)
WEnd

One question anyway : as I flush the file every 5sec, should I let the fileopen/fileclose inside the loop, or simply open it at beginning and letting it opened ? Shouldn't avoid intranet server to read it, right ?

Will look at EmptyWorkingSet function.


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites
kiboost

If you put the FileOpen outside the loop, writing will append to the end of the file and not overwrite it.

ok thanks

Do you think I should have same incremental ram problem with pure autoit script like this ?

And finnally, do you think the sleep5000 is needed, or could I simply let it doing the loops instaneously (maybe some writing timing will cause problem ?)


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites
Mat

You shouldn't.

The sleep 5000 is probably a good thing, as lot's of writing etc will have a cpu usage. I don't think it's necessary to have it always looping given what you are trying to do. Maybe sleep 1000 would be better as there will be processes that don't last 5 secs.

Share this post


Link to post
Share on other sites
James

The sleep 5000 is probably a good thing, as lot's of writing etc will have a cpu usage. I don't think it's necessary to have it always looping given what you are trying to do. Maybe sleep 1000 would be better as there will be processes that don't last 5 secs.

I don't know whether this would counteract itself, but Kib could do only write to the file when a new process opens.

The counteract would be the chance to close the file, relieving the resource, when it's finished the write, but a constant loop (still with a sleep of a few seconds (maybe even half a second)) would still use the resource the file left free when it was closed.

I have no idea if that will make sense, long day.

Share this post


Link to post
Share on other sites
kiboost

ok, new problem with my script, two days on this one and very sressfull !

We have now gone to windows 7, and I have big problem with session ID. My script run at startup in user session, and I need to know which session ID has the user, even locked.

So I run a quser and write the ID number in my text file (I need so then I can remotely start problem in user session).

But around ten minutes after starting and running fine, the script doesn't work anymore. process still run but write nothing.

Here is the script :

AutoItSetOption (Opt("TrayIconHide", 1) )

$kname = @ComputerName

FileChangeDir("\\demeter\Documents\_RenderfarmCache\")
FileWriteLine($kname & ".txt",$kname)

$file_path = "\\demeter\Documents\_RenderfarmCache\" & $kname & ".txt"

global $Foo, $return, $returncr, $ID


While 1

    $gotid = 0
    $Foo = Run("quser", @ScriptDir, @SW_HIDE, 2)

    While 1
        $return &= StdoutRead($Foo)
        If @error Then ExitLoop
    WEnd

    sleep(1000)

    if @error <= 0 Then
        $returncr = StringSplit($return, @CRLF,1)
        for $i = 0 to $returncr[0] Step 1
            If StringInStr($returncr[$i], @UserName) Then
                $ID = StringRegExp($returncr[$i], '( [0-9] )', 1)
                $gotid = 1
                ExitLoop
            EndIf
        Next
    EndIf



    $list = ProcessList()
    $Return = $kname

    for $i = 1 to $list[0][0]
        $Return = $Return & @CRLF & $list[$i][0]
    next

    If $gotid = 1 Then
        $Return = $ID[0] & @CRLF & $Return
    EndIf

    $file = FileOpen($file_path, 2)
    FileWriteLine($file, $Return)
    FileFlush($file)
    FileClose($file)

    Sleep(5000)
WEnd

I guess a none cmd request with some wmi code would be better, but I know nothing about that :/

Any idea ? I just need to check user is session and write it at top of the process list.


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites
kiboost

Seems I've found the solution, with some part from Ascend4nt ProcessListWTS !

Global $_PFhKern32DLL=DllOpen("kernel32.dll")
$vProcessID = "xxx.exe"
Func _ProcessGetSessionID($vProcessID)
    If Not IsNumber($vProcessID) Then
        $vProcessID=ProcessExists($vProcessID)
        If $vProcessID=0 Then Return SetError(1,0,-1)
    EndIf
    Local $aRet=DllCall($_PFhKern32DLL,"bool","ProcessIdToSessionId","dword",$vProcessID,"dword*",0)
    If @error Then Return SetError(2,@error,-1)
    If Not $aRet[0] Then Return SetError(3,0,-1)
    Return $aRet[2]
EndFunc

then later in the script : $Return = _ProcessGetSessionID($vProcessID) & @CRLF & $Return


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.