Jump to content

Embedded IE memory hog


Recommended Posts

I'm trying to automate a webpage, but I have a condition to use embedded IE, not regular IE.

I have made a demo script which will go through several pages in Embed IE, and after it is completed my script's exe is containing additional ~200MB memory usage in Task Manager. And if I run the browsing process again, my script will get additional +200mb to a total of ~400mb, and so on...
I'm not sure why is this happening. If I use normal IE (I've made a test with the normal IE in this script too, just replace usage of lines 19 and 20) it is not using as nearly resource memory as the embedded one.

Is there a way to clear all this memory usage from my script?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <IE.au3>

Global $oIE2, $SinkObject2, $IEhwnd2 = 0, $oIEobj2

$Form1 = GUICreate("IE Loaded: 0%", 320, 55, 10, 10)
$Button1 = GUICtrlCreateButton('start IE', 10, 10, 300, 35)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")
GUIRegisterMsg($WM_SIZE, "WM_SIZE")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            _StartTheEmbedBrowser(1)
;~          _StartTheNormalBrowser(1)
            If @error Then
                MsgBox(0, 'Error:', 'Error: ' & @error)
            Else
                _StartSliding()
            EndIf
    EndSwitch
WEnd

Func WM_SYSCOMMAND($hWnd, $iMsg, $wParam, $lParam)
    Switch $hWnd
        Case $Form1, $IEhwnd2
            Switch $wParam
                Case $SC_CLOSE
                    Exit
            EndSwitch
        EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

Func WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam
    Switch $hWnd
        Case $IEhwnd2
            Local $iWidth = BitAND($ilParam, 0x0000FFFF)
            Local $iHeight = BitShift($ilParam, 16)

            GUICtrlSetPos($oIEobj2, 0, 0, $iWidth, $iHeight)
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

Func _StartTheNormalBrowser($visible = 0, $focus = 1)
    If IsObj($oIE2) then _IEQuit($oIE2)
    If WinGetHandle($IEhwnd2) then WinClose($IEhwnd2)
    $oIE2 = _IECreate('about:blank', 0, $visible, -1, $focus)
    If @error then Return SetError(1)
    If NOT IsObj($oIE2) then Return SetError(1)
    $SinkObject2 = ObjEvent($oIE2, "IEEvent_", "DWebBrowserEvents2")
    $IEhwnd2 = _IEPropertyGet($oIE2, "hwnd")
    If NOT WinGetHandle($IEhwnd2) then Return SetError(1)
EndFunc

Func _StartTheEmbedBrowser($visible = 0, $focus = 1)
    Local $EmbedIE_Width = 900
    Local $EmbedIE_Height = 600
    If WinExists($IEhwnd2) Then GUIDelete($IEhwnd2)
    $oIE2 = ObjCreate("Shell.Explorer.2")
    If @error Then Return SetError(1)
    $IEhwnd2 = GUICreate('WEB navigation', $EmbedIE_Width, $EmbedIE_Height, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_POPUPWINDOW, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS))
    $oIEobj2 = GUICtrlCreateObj($oIE2, 0, 0, $EmbedIE_Width, $EmbedIE_Height)
    If Not IsObj($oIE2) Then Return SetError(1)
    $SinkObject2 = ObjEvent($oIE2, "IEEvent_", "DWebBrowserEvents2")
    $oIE2.document.body.scroll = "yes"
    If $visible > 0 Then GUISetState()
EndFunc

Func _StartSliding()
    _IENavigate($oIE2, 'http://www.tripadvisor.com/Restaurants-g2697380-Western_Romania_Transylvania.html');the webpage was randomly chosen to demonstrate the issue
    Local $theEnd = False
    Local $FirstRun = True
    Do
        Local $oDivs = _IETagNameGetCollection($oIE2, 'div')
        For $oDiv in $oDivs
            If $oDiv.classname == 'deckTools btm' Then
                Local $oSpans = _IETagNameGetCollection($oDiv, 'span')
                For $oSpan in $oSpans
                    If $oSpan.classname == 'pgCount' Then
                        Local $splitText = StringSplit($oSpan.innerText, ' of ', 1)
                        If NOT @error Then
                            For $m = 1 to $splitText[0]
                                If $m = 1 Then
                                    Local $splitNumber = StringSplit($splitText[$m], '-')
                                    If NOT @error Then
                                        If ($splitNumber[0] >= 2) AND ($splitText[0] >= 2) then
                                            $splitText[2] = StringReplace($splitText[2], ',', '')
                                            $TotalRestaurants = $splitText[2]
                                            WinSetTitle($IEhwnd2, '', 'WEB navigation - ' & 'my current number: ' & $splitNumber[2] & ', end page number: ' & $splitText[2])
                                            If $FirstRun = True then
                                                If Number($splitNumber[2]) >= Number($splitText[2]) then
                                                    If $FirstRun = True Then
                                                        $FirstRun = False
                                                    Else
                                                        $theEnd = True
                                                    EndIf
                                                EndIf
                                            Else
                                                If Number($splitNumber[2]) <= 60 then $theEnd = True
                                            EndIf
                                        EndIf
                                    EndIf
                                EndIf
                            Next
                        EndIf
                    EndIf
                Next
            EndIf
        Next

        If $theEnd = False then
            Local $AllA2 = _IETagNameGetCollection($oIE2, 'a')
            For $OneA2 in $AllA2
                If $FirstRun = True then
                    If StringInStr($oneA2.classname, 'guiArw sprite-pageNext') then
                        _IENavigate($oIE2, $oneA2.href)
                        Sleep(1000)
                    EndIf
                Else
                    If StringInStr($oneA2.classname, 'guiArw sprite-pagePrev') then
                        _IENavigate($oIE2, $oneA2.href)
                        Sleep(1000)
                    EndIf
                EndIf
            Next
        EndIf
        Sleep(100)
    Until $theEnd = True
    MsgBox(0, 'Done', 'Completed', 10, $Form1)
    $oIE2.quit()
    $oIE2 = 0
    $oIEobj2 = 0
    GUIDelete($IEhwnd2)
EndFunc

Func IEEvent_ProgressChange($Progress, $ProgressMax)
    $percent = Int(($Progress * 100) / $ProgressMax)
    If $percent >= 0 And $percent <= 100 Then WinSetTitle($Form1, '', 'IE Loaded: ' & $percent & '%')
EndFunc
Link to comment
Share on other sites

Thanks for the info JohnOne.

I have tried the emptyworkingset function, it is somewhat helpfull, but not for long-term run because the script's memory usage just keep getting bigger and bigger after each run (but in much much slower rate than before).

I'll see what else can I do with the embedded IE to reduce it's memory usage.

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...