我正在製作瀏覽器遊戲的機器人。我正在使用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
}
}
將恢復到版本1.7.3刪除Awesomium(多線程,JavaScript回調等)的任何主要功能? – user2155059
恢復到1.7.3將刪除1.7.4中添加的Mutli線程,但同時JavaScript回調已經成爲Awesomium很長一段時間的一部分。 –
好的,謝謝你@James LaPenn – user2155059