2014-04-07 125 views
0

我正在製作瀏覽器遊戲的機器人。我正在使用http://awesomium.com連接到該頁面。 我使用webview來顯示curstom表單上的頁面,但問題是,經過很長時間,awesomium_process達到200k的內存使用量,並不斷上升.. 我不知道如何減少內存使用..(我無法使用webcontrol,因爲injectmousemove不會對webcontrol中的flash內容造成影響)Awesomium減少webview內存使用量

任何人都可以幫我嗎?

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Awesomium.Core; 
using Awesomium.Windows.Forms; 
using Awesomium.Web; 
using System.Diagnostics; 
using System.IO; 

namespace ProBot 
{ 
public partial class Explorer : UserControl 
{ 

    private WebView webView; 
    private ImageSurface surface; 
    private WebSession session; 
    public Explorer() 
    { 
     session = InitializeCoreAndSession(); 
     InitializeComponent(); 
     InitializeView(WebCore.CreateWebView(this.ClientSize.Width, this.ClientSize.Height, session)); 
    } 

    #region Metodos 

    private void InitializeView(WebView view) 
    { 
     if (view == null) 
      return; 

     surface = new ImageSurface(); 
     surface.Updated += OnSurfaceUpdated; 

     webView = view; 
     webView.Surface = surface; 
     webView.Source = "http://google.com".ToUri(); 
     webView.FocusView(); 
    } 

    private WebSession InitializeCoreAndSession() 
    { 
     if (!WebCore.IsInitialized) 
      WebCore.Initialize(new WebConfig() { LogLevel = LogLevel.Normal, ReduceMemoryUsageOnNavigation = true }); 

     // Build a data path string. In this case, a Cache folder under our executing directory. 
     // - If the folder does not exist, it will be created. 
     // - The path should always point to a writeable location. 
     string dataPath = String.Format("{0}{1}Cache", Path.GetDirectoryName(Application.ExecutablePath), Path.DirectorySeparatorChar); 

     // Check if a session synchronizing to this data path, is already created; 
     // if not, create a new one. 
     session = WebCore.Sessions[dataPath] ?? 
      WebCore.CreateWebSession(dataPath, WebPreferences.Default); 

     // The core must be initialized by now. Print the core version. 
     Debug.Print(WebCore.Version.ToString()); 

     // Return the session. 
     return session; 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     if ((surface != null) && (surface.Image != null)) 
     { 
      e.Graphics.DrawImageUnscaled(surface.Image, 0, 0); 
     } 
     else 
      base.OnPaint(e); 
    } 

    protected override void OnResize(EventArgs e) 
    { 
     base.OnResize(e); 

     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.IsRendering = (this.ClientSize.Width > 0) && (this.ClientSize.Height > 0); 
     if (webView.IsRendering) 
      webView.Resize(this.ClientSize.Width, this.ClientSize.Height); 
    } 

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     base.OnMouseMove(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectMouseMove(e.X, e.Y); 
    } 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     base.OnMouseDown(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectMouseDown(e.Button.GetMouseButton()); 
    } 

    protected override void OnMouseUp(MouseEventArgs e) 
    { 
     base.OnMouseUp(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectMouseUp(e.Button.GetMouseButton()); 
    } 

    protected override void OnMouseWheel(MouseEventArgs e) 
    { 
     base.OnMouseWheel(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectMouseWheel(e.Delta, 0); 
    } 

    protected override void OnKeyPress(KeyPressEventArgs e) 
    { 
     base.OnKeyPress(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectKeyboardEvent(e.GetKeyboardEvent()); 
    } 

    protected override void OnKeyDown(KeyEventArgs e) 
    { 
     base.OnKeyDown(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectKeyboardEvent(e.GetKeyboardEvent(WebKeyboardEventType.KeyDown)); 
    } 

    protected override void OnKeyUp(KeyEventArgs e) 
    { 
     base.OnKeyUp(e); 
     if ((webView == null) || (!webView.IsLive)) 
      return; 

     webView.InjectKeyboardEvent(e.GetKeyboardEvent(WebKeyboardEventType.KeyUp)); 
    } 

    #endregion 

    #region Eventos 
    private void OnSurfaceUpdated(object sender, SurfaceUpdatedEventArgs e) 
    { 
      Invalidate(e.DirtyRegion.ToRectangle(), false); 
    } 
    #endregion 

    #region Publics 

    private delegate void InstarMouseMoverCallback(int x, int y); 
    public void InsertarMouseMover(int x, int y) 
    { 
     if (InvokeRequired) 
     { 
      InstarMouseMoverCallback method = InsertarMouseMover; 
      Invoke(method, new object[] { x, y }); 
     } 
     else 
     { 
      webView.InjectMouseMove(x, y); 
     } 
    } 

    private delegate void InstarMouseClickIzquierdoCallback(); 
    public void InsertarMouseClickIzquierdo() 
    { 
     if (InvokeRequired) 
     { 
      InstarMouseClickIzquierdoCallback method = InsertarMouseClickIzquierdo; 
      Invoke(method, new object[] {}); 
     } 
     else 
     { 
      webView.InjectMouseDown(MouseButton.Left); 
      webView.InjectMouseUp(MouseButton.Left); 
     } 
    } 

    Bitmap captura; 
    private delegate Bitmap CapturaCallback(); 
    public Bitmap Captura() 
    { 
     if (InvokeRequired) 
     { 
      CapturaCallback method = Captura; 
      Invoke(method, new object[] { }); 
     } 
     else 
     { 
      captura = new Bitmap(surface.Image); 
     } 
     return captura; 
    } 

    private delegate void ReducirMemoriaCallback(); 
    public void ReducirMemoria() 
    { 
     if (InvokeRequired) 
     { 
      ReducirMemoriaCallback method = ReducirMemoria; 
      Invoke(method, new object[] { }); 
     } 
     else 
     { 
      webView.ReduceMemoryUsage(); 
     } 
    } 

    private delegate void CerrarCoreCallback(); 
    public void CerrarCore() 
    { 
     if (InvokeRequired) 
     { 
      CerrarCoreCallback method = CerrarCore; 
      Invoke(method, new object[] { }); 
     } 
     else 
     { 
      WebCore.Shutdown(); 
     } 

    } 

    public void CambiarPagina(String pagina) 
    { 
     webView.Source = new Uri(pagina); 
    } 
    #endregion 


} 
} 

回答

0

有幾件事情可以做,以改善這種情況,但它也取決於你有什麼地方努力,你的遊戲是如何構建的。

如果您使用的是awesomium 1.7.4,您可能會考慮恢復到Awesomium 1.7.3,新awesomium中的內存使用量已大幅增加。

您可以使用內存分析器來查看是否正在創建任何影響內存使用情況的內存泄漏。

當awesomium進程的大小增加時,一個原因是因爲javascript執行在頁面上創建對象,請確認您沒有在您設置的任何javascript代碼中泄漏內存。

Flash也可能泄漏內存,請確認您沒有泄漏任何內存。

Web上有很多工具可用於分析這些不同的組件。我已經使用ANTS進行了.NET的內存分析,我發現它非常有用和信息豐富。

如果您運行1.7.4,我會採取的第一步是嘗試1.7.3,因爲它可能是一個相當簡單的更改。我最近(實際上昨天是這篇文章)必須將我的代碼庫移回到1.7.3,因爲1.7.4中的內存問題我確信這些問題將得到解決,但我現在需要穩定。

+0

將恢復到版本1.7.3刪除Awesomium(多線程,JavaScript回調等)的任何主要功能? – user2155059

+0

恢復到1.7.3將刪除1.7.4中添加的Mutli線程,但同時JavaScript回調已經成爲Awesomium很長一段時間的一部分。 –

+0

好的,謝謝你@James LaPenn – user2155059