zoukankan      html  css  js  c++  java
  • 如何知道linux程序被kill的原因

    运行一个python程序,用的无限运行,但老是运行不起来,nohup后台运行查不出原因。
    随改成前台运行python,发现程序在某个地方被killed了,百度知道可以查/var/log/message,终于知道原因了:

        May 20 19:45:59 VM_0_5_centos kernel: Out of memory: Kill process 10114 (python3) score 821 or sacrifice 
        child
        May 20 19:45:59 VM_0_5_centos kernel: Killed process 10114 (python3) total-vm:3504040kB, anon- 
        rss:3278700kB, file-rss:4kB, shmem-rss:0kB
    

    内存溢出了,直接在内存溢出的地方改逻辑,程序就愉快地继续运行了。
    这个地方的逻辑是:

        if os.path.exists('istiebaimgget.txt'):
            with open('istiebaimgget.txt','r',encoding='utf-8') as f:
                for i in f.readlines():
                    i = i.replace('
    ','').replace('
    ','')
                    if len(i) > 0:
                        istiebaimgget.add(i)
                    if len(istiebaimgget) > 600000:
                        break
        imgdata.clear()
    

    这个地方的istiebaimgget.txt文件太大撑满内存的话,程序就不给运行了。。。
    改成如下的格式:

        if os.path.exists('istiebaimgget.txt'):
            if(os.path.getsize('istiebaimgget.txt') > 600000):
                os.remove('istiebaimgget.txt')
            else:
                with open('istiebaimgget.txt','r',encoding='utf-8') as f:
                    for i in f.readlines():
                        i = i.replace('
    ','').replace('
    ','')
                        if len(i) > 0:
                            istiebaimgget.add(i)
                        if len(istiebaimgget) > 600000:
                            break
        imgdata.clear()
    

    程序正常运行了,超过2G的文件被删除了,内存不会溢出了。

  • 相关阅读:
    JAVA 接口与抽象类(interface与abstract)的区别
    接口测试的测试点
    HTTP协议首部及Fiddler工具工作原理
    Android自动化测试AppiumLibrary库关键字
    蔬菜水果购买记
    健胃饮食
    榨汁机食谱
    随机权值平均
    周鸿祎IOT发布会思考
    Bilinear CNN与 Randomly Wired Neural Network
  • 原文地址:https://www.cnblogs.com/dgutfly/p/14791407.html
Copyright © 2011-2022 走看看