我正在为浏览器游戏制作机器人。我正在使用http://awesomium.com来连接到该页面。 我使用webview在curstom表单上显示页面,但问题是,经过很长一段时间,awesomium_process达到了200k的内存使用量并且它不断提升.. 我不知道如何减少内存使用量...(我不能使用webcontrol,因为injectmousemove不会对webcontrol中的flash内容产生影响)
任何人都可以帮助我吗?
这是我的代码:
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 :(得分:0)
您可以采取一些措施来改善这种状况,但这也取决于您正在使用的内容以及游戏的构建方式。
如果您使用的是awesomium 1.7.4,您可能会考虑恢复到Awesomium 1.7.3,新的awesomium中的内存使用量大幅增加。
您可以使用内存分析器查看是否正在创建影响内存使用的任何内存泄漏。
当awesomium进程的大小增加时,一个原因是因为javascript执行在页面上创建对象,请确认您没有在已设置的任何javascript代码中泄漏内存。
Flash也可能泄漏内存,确认您没有泄漏任何内存。
网上有很多工具可用于分析这些不同的组件。我已经使用ANTS进行.NET的内存分析,我发现它非常有用且信息丰富。
如果您正在运行1.7.4,我将采取的第一步是尝试1.7.3,因为它可能是一个相当简单的更改。我最近(实际上这篇文章的昨天)由于1.7.4中的内存问题而不得不将我的代码库移回1.7.3我确定这些问题将得到修复,但我现在需要稳定。