python中MemoryError问题解决

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012111465/article/details/80343325

在使用python的surprise库的KNN时,出现内存问题,因为KNN要大量计算相似度,需要消耗大量的内存,之后发现是自己电脑内存太低,尤其是32位的电脑,系统内存是被分配好的,64位的好一些,删除了一些软件之后,内存还是不足,借用朋友的电脑,一切都ok了。因此,在服务器上或者内存足够的情况下,不用担心内存问题。

求助pythonMemoryError问题

03-28

在一个项目中,我使用了python日志中的RotatingFileHandler,但是这个有一个bug,所以我自定义了一个类继承,并修改了其中的方法,不过现在有一个这样的问题,当程序连续运行2,3天,就会报如下的错误:rn[code=Python]Traceback (most recent call last):rn File "logging\__init__.pyc", line 776, in emitrn File "logging\__init__.pyc", line 654, in formatrn File "logging\__init__.pyc", line 438, in formatrn File "logging\__init__.pyc", line 404, in formatTimern MemoryError[/code]rnrn其中我写的自定义日志类如下:rn[code=Python]import codecsrnimport loggingrnfrom logging.handlers import RotatingFileHandlerrnimport osrnrnclass customLog(RotatingFileHandler):rn def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):rn if maxBytes > 0:rn mode = 'a' # doesn't make sense otherwise!rn self.maxBytes = maxBytesrn self.backupCount = backupCountrn RotatingFileHandler.__init__(self, filename, mode, maxBytes, backupCount, encoding,delay)rnrnrn def doRollover(self):rn """rn Do a rollover, as described in __init__().rn """rnrn self.stream.close()rn try:rn if self.backupCount > 0:rn for i in range(self.backupCount - 1, 0, -1):rn sfn = "%s.%d" % (self.baseFilename, i)rn dfn = "%s.%d" % (self.baseFilename, i + 1)rn if os.path.exists(sfn):rn # print "%s -> %s" % (sfn, dfn)rn if os.path.exists(dfn):rn os.remove(dfn)rn os.rename(sfn, dfn)rn dfn = self.baseFilename + ".1"rn if os.path.exists(dfn):rn os.remove(dfn)rn os.rename(self.baseFilename, dfn)rn # print "%s -> %s" % (self.baseFilename, dfn)rn except Exception:rn passrn #print "doRollover"rn finally:rn if self.encoding:rn self.stream = codecs.open(self.baseFilename, 'w', self.encoding)rn else:rn self.stream = open(self.baseFilename, 'w')rnrn def shouldRollover(self, record):rn """rn Determine if rollover should occur.rnrn Basically, see if the supplied record would cause the file to exceedrn the size limit we have.rn """rnrn if self.stream is None: # delay was set...rn self.stream = self._open()rn if self.maxBytes > 0: # are we rolling over?rn msg = ""rn try:rn msg = "%s\n" % self.format(record)rn self.stream.seek(0, 2) #due to non-posix-compliant Windows featurern except :rn print "shouldRollover"rn if self.stream.tell() + len(msg) >= self.maxBytes:rn return 1rn return 0rnrn def emit(self, record):rn """rn Emit a record.rnrn Output the record to the file, catering for rollover as describedrn in doRollover().rn """rnrn try:rn if self.shouldRollover(record):rn self.doRollover()rn logging.FileHandler.emit(self, record)rn except (KeyboardInterrupt, SystemExit):rn print "error"rn #raisern except:rn self.handleError(record)[/code]rn请问,这个问题是什么问题造成的。

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭