This topic contains the following sections.
The .NET Framework provides automatic memory management that allocates and releases memory as your application runs. However, VisionPro uses large blocks of unmanaged memory to store acquired images. Since the .NET Framework is not aware of the unmanaged memory, it is possible to use up the unmanaged heap before the .NET Framework runs the garbage collector. To prevent this problem you must periodically invoke the garbage collector yourself to free both the managed and unmanaged memory for images that are no longer being used.
Cognex recommends that you call GC.Collect after several acquisitions. The sample code typically calls the garbage collector after four acquisitions. You will want to tune the frequency to take into account the needs of your application.
C# Code
private void AcquireAndDisplay() {
int trignum;
cogDisplay1.Image = myAcqFifo.Acquire(out trignum);
numAcqs++;
if (numAcqs > 4) {
GC.Collect();
numAcqs = 0;
}
}VB.NET Code
Private Sub AcquireAndDisplay()
Dim trignum As Integer
cogDisplay1.Image = myAcqFifo.Acquire(trignum)
numAcqs += 1
If numAcqs > 4 Then
GC.Collect()
numAcqs = 0
End If
End SubSome VisionPro object will perform the periodic garbage collection automatically when they are configured to do so. If you use automatic garbage collection, do not call GC.Collect yourself. Calling the garbage collector too frequently reduces the overall performance of your application.
The CogAcqFifoTool performs garbage collection if GarbageCollectionEnabled is set to True. The GarbageCollectionFrequency property specifies how many images the tool acquires before it invokes the garbage collector. When a CogAcqFifoTool is created within the QuickBuild application, it is configured to called the garbage collector every fifth run of the tool.
The CogToolGroup performs garbage collection if GarbageCollectionEnabled is set to True. The GarbageCollectionFrequency property specifies how many times the tool group runs before it invokes the garbage collector.
By default, garbage collection is disabled for all tool groups except those created in the VisionPro QuickBuild application. Within QuickBuild the tool group is configured to call the garbage collector after every 15 calls to the tool group's Run method.
CogJobManager performs garbage collection for QuickBuild jobs if GarbageCollection is set to True. The GarbageCollectionInterval property specifies how many images a job acquires before it invokes the garbage collector. If RunMode is CogJobRunModeConstants this property specifies the number of times that the vision tool runs before calling the garbage collector.