如何修复我的应用程序中的内存泄漏? [英] How to fix memory leak in my application?

查看:87
本文介绍了如何修复我的应用程序中的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GAE应用程序中,我向Google Spreadsheet添加行.

In my GAE app I add rows to Google Spreadsheet.

taskqueue.add(url='/tabletask?u=%s' % (user_id),
retry_options=taskqueue.TaskRetryOptions(task_retry_limit=0),
                            method='GET')

class TableTaskHandler(webapp2.RequestHandler):
    def get(self):
        user_id = self.request.get('u')
        if user_id:
            try:
                tables.add_row(
                    user_id
                )
            except Exception, error_message:
                pass



def get_google_api_service(scope='https://www.googleapis.com/auth/spreadsheets', api='sheets', version='v4'):
    ''' Login to Google API with service account and get the service
    '''
    service = None
    try:
        credentials = AppAssertionCredentials(scope=scope)
        http = credentials.authorize(httplib2.Http(memcache))
        service = build(api, version, http=http)
    except Exception, error_message:
        logging.exception('Failed to get Google API service, exception happened - %s' % error_message)
    return service

def add_row(user_id, user_name, project_id, question, answer, ss_id=SPREADSHEET_ID):
    service = get_google_api_service()
    if service:
        values = [
            [
                user_id, user_name, project_id, question, answer # 'test1', 'test2'
            ],
            # Additional rows ...
        ]
        body = {
            'values': values
        }
        # https://developers.google.com/sheets/api/guides/values#appending_values
        response = service.spreadsheets().values().append(
            spreadsheetId=ss_id,
            range='A1:E1000',
            valueInputOption='RAW',
            body=body).execute()

我添加了许多具有不同行值的任务.

I add many tasks with different row values.

结果是,总共为5个请求提供服务后,我收到严重错误超出了128 Mb的软专用限制,为158 Mb".

In result I get critical errors 'Exceeded soft private limit of 128 Mb with 158 Mb' after servicing 5 requests in total.

这可能是什么问题?

推荐答案

乍一看,您的代码中没有什么特别之处,可能会导致内存泄漏. 我认为除非他非常熟悉所使用的第3方库及其存在的错误,否则没人能找到它.因此,我将按照以下方式解决该问题:

At first glance there’s nothing special in your code that might lead to a memory leak. I don’t think anybody can locate it unless he’s very deeply familiar with the 3rd party libraries used and their existing bugs. So I’d approach the problem as follows:

  1. 首先让我们找出恰好的内存泄漏位置,以及它是否完全泄漏了.

  1. First lets find out where exactly memory is leaking and whether it’s leaking at all.

请参阅 tracemalloc 很重或您熟悉的其他任何内容. 建议使用哪种Python内存探查器?

Refer to tracemalloc, memory_profiler, heapy or whatever else you’re familiar with. Most profilers available are listed here Which Python memory profiler is recommended?

预期结果:您清楚地知道哪里内存正在泄漏,直到代码行/python表达式为止

Expected outcome: you clearly know where exactly the memory is leaking, up to a code line / python expression

如果问题出在第三方代码中,请尝试更深入地研究其代码并找出问题所在

If the problem is in a 3rd party code, try to dig deeper into its code and figure out what’s up there

取决于p.2的结果

a.发布另一个SO问题,例如为什么此python代码摘录会导致内存泄漏"-理想情况下,它应该是一个独立的代码段,该行为不会产生任何第三方库且可在本地复制的怪异行为.环境规范-至少需要python版本,谢谢

a. Post another SO question like ‘why this python code excerpt leads to a memory leak’ - ideally it should be a standalone code snippet that leads to a weird behavior free of any third party libraries and reproducible locally. Environment specification - at least python version, is appreciated

b.如果问题出在第三方库中,并且您已找到问题,请在托管目标项目的github/任何地方打开错误报告

b. If the problem is in a 3rd party library and you’ve located the problem, open a bug report on github/anywhere the target project is hosted

c.如果问题很明显是在第三方库中,而您找不到原因,请打开描述该案例的故障单,并附上分析器的报告

c. If the problem is clearly in a 3rd party library and you’re unable to find the cause, open a ticket describing the case with the profiler's report attached

这篇关于如何修复我的应用程序中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆