TensorFlow Serving内存泄漏问题定位与解决
在使用TensorFlow Serving进行模型服务化部署时,我们遇到了一个棘手的内存泄漏问题。该问题在生产环境中表现为服务内存持续增长,最终导致服务崩溃。
问题复现步骤
首先,在Docker容器中部署TensorFlow Serving服务:
FROM tensorflow/serving:latest
COPY model /models/my_model
ENV MODEL_NAME=my_model
EXPOSE 8500 8501
ENTRYPOINT ["tensorflow_model_server"]
通过以下命令启动容器:
sudo docker run -d --name tf_serving \n -p 8500:8500 -p 8501:8501 \n --memory=2g \n tensorflow/serving:latest \n --model_base_path=/models/my_model \n --rest_api_port=8500 \n --grpc_port=8501
根本原因分析
经过深入排查,发现内存泄漏主要由以下两个因素导致:
- 模型版本管理不当:在频繁更新模型时,旧版本模型未被正确清理,导致多个版本同时驻留内存
- 请求处理超时设置不合理:默认的超时设置使得长时间运行的请求占用内存资源
解决方案
# 优化启动参数
--model_base_path=/models/my_model \n--rest_api_port=8500 \n--grpc_port=8501 \n--model_version_policy='{"latest": {"num_versions": 1}}' \n--enable_batching=true \n--batching_parameters_file=/batching_config.txt
同时,在Docker Compose配置中加入内存限制和健康检查:
services:
tensorflow-serving:
image: tensorflow/serving:latest
deploy:
resources:
limits:
memory: 2G
reservations:
memory: 1G
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8500/healthz"]
interval: 30s
timeout: 10s
retries: 3
负载均衡配置
为了提高服务稳定性,我们采用Nginx进行负载均衡:
upstream tensorflow_servers {
server 127.0.0.1:8500;
server 127.0.0.1:8501;
}
server {
listen 80;
location / {
proxy_pass http://tensorflow_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

讨论