我在更新使用GC.Collect()进行强制释放,但是不起作用,就在用到了。win32:SetProcessWorkingSetSize,进行释放,内存是不上升了,但是过不了多久,程序出现自动退出。这个是什么原因?求解
1 this.programState = programState; 2 this.needClose = needClose; 3 cameraHandle = 0; 4 //该函数打开摄像头,一个给定的名称,并返回其手柄。 5 int r = FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle); 6 if (r != FSDK.FSDKE_OK) 7 { 8 errorMsg = "打开摄像头错误,检查是否被其他程序占用。"; 9 } 10 else 11 { 12 errorMsg = null; 13 } 14 //设置实时人脸检测参数 15 FSDK.SetFaceDetectionParameters(false, true, 200); 16 FSDK.SetFaceDetectionThreshold((int)FaceDetectionThreshold); 17 18 //矩形框X、Y坐标 19 int X, Y = 0; 20 Speech("请看着摄像头"); 21 //画面集合 22 List<FSDK.CImage> imageList = new List<FSDK.CImage>(); 23 while (!this.needClose) 24 { 25 string userName = null; 26 float similarity = 0; 27 Int32 imageHandle = 0; 28 if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current Faceame Faceom the camera 29 { 30 Application.DoEvents(); 31 continue; 32 } 33 //事实画面图像 34 FSDK.CImage image = new FSDK.CImage(imageHandle); 35 Image FaceameImage = image.ToCLRImage(); 36 FSDK.TFacePosition facePosition = image.DetectFace(); 37 Graphics gr = Graphics.FromImage(FaceameImage); 38 if (facePosition.w != 0) 39 { 40 X = facePosition.xc - facePosition.w / 2; 41 Y = facePosition.yc - facePosition.w / 2; 42 switch (this.programState) 43 { 44 case ProgramState.ReadyRemember: //准备拍摄 45 ReadyRemember = true; 46 ReadyState(gr, facePosition, X, Y, "准备拍摄 ", image); 47 ISpeech = 0; 48 break; 49 case ProgramState.Remember: //拍摄状态 50 ReadyRemember = false; 51 TFaceRecord face = null; 52 faceRecord(FaceameImage, out face); 53 ISpeech = 0; 54 this.programState = ProgramState.Recognize; 55 //拍照后事件 56 PhotographResult(this, new FaceEventArgs(face, true)); 57 break; 58 case ProgramState.ReadyRecognize://准备识别 59 //检测五官位置 60 FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition); 61 gr.DrawRectangle(Pens.LightGreen, X, Y, facePosition.w, facePosition.w); 62 //两只眼睛Y坐标 63 if (Math.Abs(facialFeatures[0].y - facialFeatures[1].y) > 20) 64 { 65 if (isstop <= 1) 66 { 67 isstop++; 68 Speech("请将头部摆正"); 69 } 70 } 71 else 72 { 73 imageList.Add(image); 74 if (imageList.Count > 5) 75 { 76 this.programState = ProgramState.Recognize; 77 } 78 if (imageList.Count > 10) 79 { 80 imageList.Clear(); 81 } 82 } 83 break; 84 case ProgramState.Recognize: // 识别用户 85 if (Recognize(imageList, gr, facePosition, X, Y, out userName, out similarity)) 86 { 87 if (ISpeech == 0) 88 { 89 if (!string.IsNullOrEmpty(userName)) 90 { 91 Speech(userName); 92 } 93 ISpeech++; 94 } 95 this.programState = ProgramState.Recognize; 96 if (imageList.Count > 10) 97 { 98 imageList.Clear(); 99 } 100 itmp = 0; 101 } 102 else 103 { 104 if (itmp <= 2) 105 { 106 // Speech("未能识别到,检查是否已经录入数据"); 107 itmp++; 108 } 109 ISpeech = 0; 110 imageList.Clear(); 111 this.programState = ProgramState.ReadyRecognize; 112 } 113 break; 114 case ProgramState.End: 115 //面部范围 116 //gr.DrawRectangle(Pens.LightGreen, X, Y, facePosition.w, facePosition.w); 117 break; 118 } 119 } 120 //显示当前帧事件 121 FaceDetectResult(this, new FaceEventArgs(FaceameImage, userName, similarity)); 122 //强制垃圾回收 123 GC.Collect(); 124 Application.DoEvents(); 125 } 126
/// <summary> /// 释放内存 /// </summary> private void ClearMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } }
现在用的是这个后面的方法进行释放的,内存没上升,说明达到释放的效果。程序运行也正常,但是昨天我在换了一台电脑运行就出现问题了,程序时间长了卡死。
不是用多线程的么?为毛要使用委托实时更新画面?
我建议你把代码贴出来看看,GC.Collect() 只是对没有引用的对象回收,可能你所使用的对象还有引用没有释放掉
之前把释放图片资源注释掉了。导致内存不断上升。。