Saya sedang mengembangkan aplikasi yang terhubung ke kamera GigEVision, dan mengambil gambar darinya. Saat ini saya menggunakan Pleora eBus SDK dengan C#.NET.
Kode di bawah ini hanyalah aplikasi pengujian untuk koneksi kamera - kode ini dapat melakukan streaming gambar, tetapi cepat kehabisan memori kecuali saya memanggil GC.Collect(); Perlu diperhatikan bahwa gambar yang dialirkan berukuran besar (4096x3072), sehingga kerusakan terjadi cukup cepat.
Awalnya saya curiga bahwa tidak menelepon Dispose() adalah masalahnya. Namun, saya dapat memanggil Dispose() pada setiap gambar tepat sebelum menghilangkan referensi ke gambar tersebut, dan itu tidak menyelesaikan masalah.
Saya juga telah mencoba secara eksplisit melepaskan buffer yang masuk ke panggilan balik thread tampilan, tetapi itu tidak berpengaruh.
Bisakah saya mendapatkan kembali ingatan saya dengan cara yang lebih elegan?
using System;
using System.Windows.Forms;
using PvDotNet;
using PvGUIDotNet;
using System.Drawing;
namespace eBus_Connection
{
public partial class MainForm : Form
{
PvDeviceGEV camera;
PvStreamGEV stream;
PvPipeline pipeline;
PvDisplayThread thread;
bool updating = false;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Shown(object sender, EventArgs e)
{
PvDeviceInfo info;
PvDeviceFinderForm form = new PvDeviceFinderForm();
form.ShowDialog();
info = form.Selected;
camera = PvDeviceGEV.CreateAndConnect(info) as PvDeviceGEV;
stream = PvStreamGEV.CreateAndOpen(info.ConnectionID) as PvStreamGEV;
pipeline = new PvPipeline(stream);
if (camera == null || stream == null)
throw new Exception("Camera or stream could not be created.");
camera.NegotiatePacketSize();
camera.SetStreamDestination(stream.LocalIPAddress, stream.LocalPort);
camera.StreamEnable();
camera.Parameters.ExecuteCommand("AcquisitionStart");
pipeline.Start();
thread = new PvDisplayThread();
thread.OnBufferDisplay += thread_OnBufferDisplay;
thread.Start(pipeline, camera.Parameters);
status.DisplayThread = thread;
status.Stream = stream;
}
void thread_OnBufferDisplay(PvDisplayThread aDisplayThread, PvBuffer aBuffer)
{
Bitmap b = new Bitmap((int)aBuffer.Image.Width, (int)aBuffer.Image.Height);
aBuffer.Image.CopyToBitmap(b);
BeginInvoke(new Action<Bitmap>(ChangeImage), b);
}
void ChangeImage(Bitmap b)
{
if (PictureBox.Image != null)
PictureBox.Dispose();
PictureBox.Image = b;
GC.Collect();//taking this away causes memory to leak rapidly.
}
}
}
PictureBox.Image.Dispose()
, bukanPictureBox.Dispose()
. - person Michael Liu   schedule 01.07.2014