程序长时间运行出现error allocating memory错误
代码如下 ,长时间运行就会出现如图错误,请大家帮忙看看,谢谢啦#cs ____________________________________
#ce _______________脚本开始_________________
#include <FTPEx.au3>
#include "CoProc.au3"
#include <misc.au3>
While 1
$iPidotherfuc = _CoProc("ftp")
;ftp()
Sleep(10000)
#CS If ProcessExists($iPidotherfuc) Then
ProcessClose($iPidotherfuc)
endif
#CE
WEnd
Func ftp()
_Singleton("ftpfunc",0)
Dim $downftpfileflag
Dim $fFailIfExists
;$server = ''
$server = ''
$username = 's1'
$pass = 'fin23'
$Open = _FTP_Open('SlipFinder ftp ')
$Conn = _FTP_Connect($Open, $server, $username, $pass)
;MsgBox(0,"$Conn",_FTP_DirGetCurrent($Conn),1)
If $Conn <> 0 Then
$aFile = _FTP_ListToArray($Conn, 2) ;只找文件
If IsArray($aFile) Then
Sleep(2000) ;等待下,以防止机台产生数据中
For $i= 1 To $aFile
;MsgBox(0,"",$aFile[$i],1)
$sourcefile = "/" & $aFile[$i]
$targetfile = "C:\Slip\" & $aFile[$i]
;$filegroup=StringSplit($aFile[$i],".")
;MsgBox(0,"",$filegroup)
;If $filegroup = "txt" Then ;如果是txt档,则下载此文件
$downftpfileflag=_FTP_FileGet($Conn,$sourcefile,$targetfile, $fFailIfExists = False,0,$INTERNET_FLAG_TRANSFER_ASCII) ;false为不覆盖
;MsgBox(0,"上传标志",$downftpfileflag ,1)
If $downftpfileflag = 1 Then ;判断是否下载成功,成功即可删除
_FTP_FileDelete($Conn, "/" & $aFile[$i])
;MsgBox(0,"文件夹名",$filegroup)
;_FTPRemovedir($Conn, "/" & $filegroup) ; 删除产生的文件夹
;MsgBox(0,"","上传后删除啦",1)
Else ;如果下载不成功,
;MsgBox(0,"@error",@error,1)
;$downftpfileflag=_FTP_FileGet($Conn,"/" & $aFile[$i] ,"C:\" & $aFile[$i], $fFailIfExists = False)
EndIf
;Else ;如果不是txt档,则直接删除
;_FTP_FileDelete($Conn, "/" & $aFile[$i])
;MsgBox(0,"","直接删除啦",1)
;EndIf
Next
EndIf
EndIf
$Ftpc = _FTP_Close($Open)
EndFunc
Func _FTPRemovedir($l_FTPSession, $sFTPDirectory)
Local $aFile, $hSearch, $sWorkingdir, $sFolderlist, $i, $bFirst, $aFolderStack =
While $aFolderStack > 0
$sWorkingdir = $aFolderStack[$aFolderStack]
$aFolderStack -= 1
$aFile = _FTP_FindFileFirst($l_FTPSession, $sWorkingdir & '/*', $hSearch, $INTERNET_FLAG_NO_CACHE_WRITE)
If Not @error Then
$bFirst = True
While 1
If $bFirst = False Then
$aFile = _FTP_FindFileNext($hSearch)
If @error Then ExitLoop
EndIf
If $aFile = 16 Then; If file is a directory we are going to add it to the stack of folders we need to go through
$aFolderStack += 1
If UBound($aFolderStack) <= $aFolderStack Then ReDim $aFolderStack
$aFolderStack[$aFolderStack] = $sWorkingdir & "/" & $aFile
$sFolderlist &= $sWorkingdir & "/" & $aFile & ';'; Here I am adding the folder to a list of directorys I need to delete later
Else; else we delete it
_FTP_FileDelete($l_FTPSession, $sWorkingdir & "/" & $aFile)
ConsoleWrite('File: ' & $sWorkingdir & "/" & $aFile & ' Deleted' & @CRLF)
EndIf
$bFirst = False
WEnd
EndIf
_FTP_FindFileClose($hSearch)
WEnd
$aFolderStack = StringSplit(StringTrimRight($sFolderlist, 1), ';')
For $i = $aFolderStack To 1 Step -1 ;Here we delete all those empty folders from last to first
_FTP_DirDelete($l_FTPSession, $aFolderStack[$i])
ConsoleWrite('Directory: ' & $aFolderStack[$i] & ' Deleted' & @CRLF)
Next
_FTP_DirDelete($l_FTPSession, $sFTPDirectory);Delete the original directory
EndFunc ;==>_FTPRemovedir
自己顶下哦,下沉了 好巧,我也有这个问题,好像内存使用过大超限 回复 3# tryhi
请问这个问题,你是怎么解决的?怎么解决的呀? 加这段释放内存代码试一下效果可否。_ReduceMemory(@AutoItPID)
Func _ReduceMemory($i_PID = -1)
If $i_PID <> -1 Then
Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle)
DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle)
Else
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndIf
Return $ai_Return
EndFunc ;==>_ReduceMemory 回复 5# 半芯蕃茄
感谢,,我测试下,估计要一个星期以后才会报错。
谢谢 回复 5# 半芯蕃茄
Dear 半芯蕃茄,加上你提供的代码,内存使用虽然大幅减小,
但一个星期下来,依然出现以上问题 , 回复 7# leon460
那看来要等高手解答了,或许你可以设定程序在运行一定时间后自动关闭后重启程序本身 楼主确定你运行出错的脚本就是上面的,没有修改?
另外,你得确定是内存漏了,还是超过使用额了:http://msdn.microsoft.com/en-us/library/aa366778(v=vs.85).aspx#memory_limits
若上传下载个超大文件(如超过500M),就可能出现这种错误。比如用_FileCountLines()函数于500M的文本,可能就需要差不多峰值3G的内存才能操作。 回复 8# 半芯蕃茄
实在没有办法了,我就只有这样做了 回复 9# happytc
所有的代码已经贴出了,呵呵,, 用过的数组要记得清掉,我写过一个就有这情况,不定时间,运行久了就无故自己退出,或者是出错,之后我把用过的临时数组在函数返回时清掉,就没有问题了,目前稳定运行几十小时不再出错。 回复 12# 曼菲士
能贴出简单的代码示例下么,谢谢 本帖最后由 曼菲士 于 2011-12-21 17:57 编辑
下面是P版的一段代码,我改了些
Local $tmpParameter = StringSplit(StringRight($PingParameter, StringLen($PingParameter) - 10), "|")
If IsArray($tmpParameter) Then
Local $aBuffer, $iUBound = UBound($aBuffer)
Local $aThread[$iUBound], $aResult[$iUBound]
For $i = 1 To $iUBound - 1
$aBuffer[$i] = DllStructCreate($tagSEND_ARP)
DllStructSetData($aBuffer[$i], "IpAddress", $tmpParameter[$i])
$aThread[$i] = _RTCreateThread($pStartAddr, DllStructGetPtr($aBuffer[$i]))
If ($aThread[$i] < 1) Then
FileWrite(@ScriptDir & "\Error.txt", "Error: " & @error & " Error creating thread, index: " & $i & @CRLF)
exit
EndIf
Next
For $i = 1 To $iUBound - 1
If _RTWaitForObject($aThread[$i]) Then
FileWrite(@ScriptDir & "\Error.txt", "Error: " & @error & " Error waiting for thread to complete, index: " & $i & @CRLF)
EndIf
_RTCloseHandle($aThread[$i])
$aResult[$i] = DllStructGetData($aBuffer[$i], "IpAddress")
$aResult[$i] = DllStructGetData($aBuffer[$i], "MacAddress")
$aResult[$i] = DllStructGetData($aBuffer[$i], "Error")
$aBuffer[$i] = 0 ;清除代码
Next
$aBuffer = 0 ;清除数组
$aThread = 0 ;清除数组
$aResult = 0 ;清除数组
EndIf
$tmpParameter = 0 ;清除数组 回复 14# 曼菲士
是不是就可以理解为 就是将数组重置为0 哈 (一行代码就够了?)
$aBuffer = 0 ;清除数组
$aThread = 0 ;清除数组
$aResult = 0 ;清除数组
$tmpParameter = 0 ;清除数组
以上例子就是你标出的部分
页:
[1]
2