为什么烧瓶中的heroku记忆增加?错误代码:H14


我英语说得不好。但我想解决这个错误。为什么heroku的记忆力不断提高?(当我使用我的网络应用程序时(

heroku错误代码是H14,如下所示:

Process running mem=541M(105.8%)
Error R14 (Memory quota exceeded)
========== after app running ===============
Process running mem=831M(162.4%)
Error R14 (Memory quota exceeded)
========== after app running ===============
Process running mem=856M(167.3%)
Error R14 (Memory quota exceeded)

我想恢复内存(?((像541M>831M>541M(,但是,内存总是增加,而不是减少,所以我最终运行heroku重新启动。

我不想重新启动,我想减少内存。我该怎么办?对不起,我英语不太好。谢谢

这是我的python代码。

from flask import Flask, render_template, request
import cv2
import numpy
import gc
import face_detection
import predict
from mtcnn.mtcnn import MTCNN
from keras.models import load_model
app = Flask(__name__,  static_url_path="/static")

print("시작 ")
detector = MTCNN()
model = load_model('./static/keras_model/1layer_128_1_best(1)-SGD.h5')
print("로딩됨")
@app.route('/')
def render_file():
return render_template('upload.html')
@app.route('/file_uploaded',methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':  # POST 방식으로 전달된 경우
f = request.files['upload_image'].read()
# # 파일 객체 혹은 파일 스트림을 가져오고, html 파일에서 넘겨지는 값의 이름을 file1으로 했기 때문에 file1임.
# 업로드된 파일을 특정 폴더에저장하고,
# convert string data to numpy array
npimg = numpy.fromstring(f, dtype=numpy.uint8)
# convert numpy array to image
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
if img.shape[0] > 500 or img.shape[1] > 500:
if img.shape[0] > img.shape[1]:
bigger = img.shape[0]
else :
bigger = img.shape[1]
scale_percent =  (bigger - 500)  *100 / bigger
width = int(img.shape[1] * (100-scale_percent) / 100)
height = int(img.shape[0] * (100- scale_percent) / 100)
dim = (width, height)
img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
print(img.shape)

# 이거때문에 face_extract 때문에 올라감
face_extract = face_detection.input_image(detector, img)
print("얼굴추출 완료")
if len(face_extract) == 0:
print("얼굴인식 못했음")
return render_template('fail_back.html')
else:
result, k = predict.prediction(face_extract, model)
iu_percent = round(float(k[0][0] * 100), 3)
suzy_percent = round(float(k[0][1]) * 100, 3)
# return send_file(file_object, mimetype='image/jpeg')
if iu_percent > suzy_percent:
return render_template('result.html', image_file="image/result_iu.jpg", not_similler="수지",
not_similler_percent=suzy_percent, similler="아이유", similler_percent=iu_percent)
else:
return render_template('result.html', image_file="image/result_suzy.jpg", not_similler="아이유",
not_similler_percent=iu_percent, similler="수지", similler_percent=suzy_percent)
else:
return render_template('fail_back.html')
if __name__ == '__main__':
# debug를 True로 세팅하면, 해당 서버 세팅 후에 코드가 바뀌어도 문제없이 실행됨.
app.run(threaded=True)

我发布了我的代码。我的代码选择一个人。它告诉两者中哪一个看起来更像。

我的应用程序站点:simstest.herokuapp.com

但是,当您使用网站时,Heroku内存会增加,网站会重新启动。

  • 哦,我找到了增加记忆力的方法。

    import gc
    def face_eye_trace(data,result_list):
    for result in result_list :
    
    x = int(result['box'][0])
    y = int(result['box'][1])
    if x < 0:
    x = 0
    if y < 0:
    y = 0
    width = int(result['box'][2])
    height = int(result['box'][3])
    img3 = data[y:y + height, x:x + width]
    return img3
    def input_image(input_detector,pixels_image):
    detector = input_detector
    pixels = pixels_image
    try:
    # 이것때문에 memory 증가===============
    faces = detector.detect_faces(pixels)
    print(len(faces))
    if len(faces) == 0:
    return []
    elif len(faces) == 1 :
    face_detection = face_eye_trace(pixels,faces)
    else :
    print("다수의 얼굴이 인식되어 종료했습니다.")
    return []
    except:
    print("얼굴 인식을 하지 못하였습니다.")
    return []
    
    print("detection:" ,gc.collect())
    return face_detection
    

此代码方法"face_detection=face_eye_trace(像素,人脸("导致内存增加。

为什么这种方法可以增加内存?为什么这个方法之后不自动返回内存?

垃圾回收器接口模块(gc(正是您可以在这里用来进一步调试这个问题的。

调试泄漏的程序调用:

gc.set_debug(gc.DEBUG_LEAK)

这将把有关垃圾收集的调试信息输出到控制台中。

为了进一步调试,gc模块提供了更多的功能,包括列出垃圾收集器跟踪的所有对象、禁用/启用收集器或手动触发垃圾收集。

如果您没有泄漏,并且只想降低内存使用率,请尝试使用gc.set_threshold在较低的内存使用点触发垃圾收集。

最新更新