问题描述
对于一个打开的串口,连续接收同一数据包,造成内存不断上涨,有什么办法可以释放因接收数据包造成不断上涨的内存?
解决方案
解决方案二:
是什么样子的啊?是一次接收很大的数据,还是每次数据少但次数多啊?我以前做过一个短信模块的,通过串口来的数据是小而次数多,所以就先把它们存到数据库了,再从数据库里读出来。不知道你的是什么样的。
解决方案三:
接受数据包难道你采用一个数组或者ArrayList等形式一致保存下来?如果是这样的话,肯定是涨内存的在我的处理中,接收到的数据放在缓冲区,如果已经处理完毕,则清除这部分的数据,内存就不怎么涨了
解决方案四:
回zhangjie_xiaoke:大概每次数据几十个byte,次数多
解决方案五:
cnming:我是采用一个静态string变量保存接收到的数据,这样可行吗?
解决方案六:
线程+缓存处理方式
解决方案七:
缓存方法能给出代码吗,谢谢
解决方案八:
再次顶起,希望能有高手给出代码
解决方案九:
静态string变量当然要涨了……
解决方案十:
缓存的意义在于这段数据处理之后就没有用了,必须从缓存清除出去,静态string恐怕不好清除把,用一个可变数组比较好,例如ArrayList,每一组指令放在ArrayList的一个Item中
解决方案十一:
#defineMAX1048575#defineBUFFERLEN5242880voidCBulkusbDlg::OnButtonWritestart(){//TODO:Addyourcontrolnotificationhandlercodehere//TODO:AddyourcontrolnotificationhandlercodehereCStringpipename;if(m_combo_bulkwritepipe.GetCurSel()==CB_ERR){MessageBox("请选择要操作的管道","警告");return;}UpdateData(true);m_BulkWrite.bInThread=TRUE;m_BulkWrite.testDlg=this;m_BulkWrite.buffersize=m_nEditWriteBufferSize;m_combo_bulkwritepipe.GetLBText(m_combo_bulkwritepipe.GetCurSel(),pipename);strcpy(m_BulkWrite.pipe_name,(LPCTSTR)pipename);m_writetransfer_info.averspeed=0;UpdateData(false);EnableButton(false,true);SetTimer(1,1000,NULL);CWinThread*wt=AfxBeginThread(BulkWritePipe,//threadfunction&m_BulkWrite);//argumenttothreadfunctionm_BulkWrite.hThread=wt->m_hThread;}voidCBulkusbDlg::OnButtonWritestop(){//TODO:Addyourcontrolnotificationhandlercodehere//TODO:Addyourcontrolnotificationhandlercodeherem_BulkWrite.bInThread=FALSE;EnableButton(true,false);KillTimer(1);m_calwritetime.bInThread=FALSE;}UINTBulkWritePipe(void*pParam){chartemp[80],cc[80];BOOLEANbResult=FALSE;intnResult=0;unsignedlongnBytes=0;unsignedlongnReadBytes=0;intnflag=0,nflag1=0;unsignedlongtotaltoTx=0,toTxNow=0,exitFlag=0,exitFlag1=0,toTxNow1=0,totaltoTx1=0;structCThreadParam*threadParam;CBulkusbDlg*testDlg;HANDLEhDevice;HANDLEhFile;unsignedchar*pbRead=newunsignedchar[1048575];threadParam=(structCThreadParam*)pParam;testDlg=threadParam->testDlg;hDevice=ISP1581_OpenDev();if(hDevice==INVALID_HANDLE_VALUE){strcpy(temp,"Opendevicehandlefailed!Can'twriteto");strcat(temp,threadParam->pipe_name);strcat(temp,".");testDlg->MessageBox((LPCSTR)temp,"TestError");testDlg->m_calwritetime.bInThread=FALSE;return0;}hFile=ISP1581_OpenPipe(threadParam->pipe_name);if(hFile==INVALID_HANDLE_VALUE){strcpy(temp,"Opendriverfailed!Can'twriteto");strcat(temp,threadParam->pipe_name);strcat(temp,".");testDlg->MessageBox((LPCSTR)temp,"TestError");testDlg->m_calwritetime.bInThread=FALSE;ISP1581_CloseDev(hDevice);return0;}HANDLEhFiles=CreateFile("c:\temp\large_file.dat",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED|FILE_ATTRIBUTE_NORMAL,NULL);unsignedlongfilelen=SetFilePointer(hFiles,0,NULL,FILE_END);curtime=timeGetTime();if(filelen>0)do{exitFlag=0;if(nflag==0)totaltoTx=filelen;do{if(totaltoTx>BUFFERLEN){totaltoTx=totaltoTx-BUFFERLEN;toTxNow=BUFFERLEN;filelen=totaltoTx;}else{toTxNow=totaltoTx;exitFlag=1;}nResult=ISP1581_Vender_ScanOrPrint(hDevice,0x80,toTxNow);if(nResult!=0){threadParam->bInThread=FALSE;wsprintf(cc,"Requestingdatatransferon");strcat(cc,threadParam->pipe_name);strcat(cc,"failed!Aborted.");testDlg->MessageBox((LPCSTR)cc,"TestError");break;}unsignedlongfilelen1=toTxNow;if(filelen1>0)do{exitFlag1=0;if(nflag1==0)totaltoTx1=filelen1;if(totaltoTx1>MAX){totaltoTx1=totaltoTx1-MAX;toTxNow1=MAX;filelen1=totaltoTx1;}else{toTxNow1=totaltoTx1;exitFlag1=1;}nReadBytes=ISP1581_Read(hFiles,pbRead,toTxNow1,1000);if(nReadBytes==-1||nReadBytes==0)break;nBytes=ISP1581_Write(hFile,pbRead,toTxNow1,1500);if(nBytes==-1||nBytes==0)break;}while(exitFlag1==0);}while(exitFlag==0);tertime=timeGetTime();usetime=tertime-curtime;testDlg->UpdateWriteInfo(usetime);threadParam->bInThread=FALSE;testDlg->m_calwritetime.bInThread=FALSE;}while(threadParam->bInThread==TRUE);testDlg->EnableButton(true,false);deletepbRead;ISP1581_ClosePipe(hFile);ISP1581_CloseDev(hDevice);return0;}unsignedlong__stdcallISP1581_Write(HANDLEpipehandle,unsignedchar*sendbuffer,unsignedlongbufferlen,intwaittime){unsignedlongnbytes=0,lasterror=0;inti=0;unsignedlongtmpbufferlen=bufferlen;OVERLAPPEDos;memset(&os,0,sizeof(OVERLAPPED));os.hEvent=::CreateEvent(NULL,TRUE,FALSE,NULL);if(!WriteFile(pipehandle,&sendbuffer[i],bufferlen,&nbytes,&os)){if((lasterror=::GetLastError())==ERROR_IO_PENDING){switch(::WaitForSingleObject(os.hEvent,(DWORD)waittime)){caseWAIT_OBJECT_0:if(!::GetOverlappedResult(pipehandle,&os,&nbytes,FALSE)){lasterror=::GetLastError();}elselasterror=0;break;caseWAIT_TIMEOUT:::CancelIo(pipehandle);lasterror=ERROR_TIMEOUT;break;default:lasterror=::GetLastError();break;}}}if(lasterror!=0){ResetPipe(pipehandle);}CloseHandle(os.hEvent);if(lasterror!=0)return-1;returnnbytes;}c++测试USB的程序原理一样的。