加载TensorFlow模型后调用tf.Session()报错,咨询内存需求
Hey there, let's work through this issue you're facing. First off, that std::system_error with "Resource temporarily unavailable" is almost certainly a memory exhaustion problem—here's why and how to fix it:
Why this happens
Your 53MB trained model file is just the serialized, compressed version of your model's structure and parameters. When TensorFlow loads it, it needs to expand those parameters (weights, biases, etc.) into in-memory tensors to execute the model. This in-memory footprint is almost always way larger than the model file size—for example, float32 parameters (the default in TF 1.x) take 4 bytes each, so even a model with 100 million parameters would take 400MB alone, not counting TensorFlow's own runtime overhead or system memory usage.
On your 1GB Ubuntu server, the OS itself is already using a chunk of memory (usually 200-300MB for a minimal server install), plus any background processes. That leaves maybe 500-600MB max for your Python and TensorFlow setup. When you load the model, it's pushing that remaining memory over the limit, which triggers the resource error when trying to initialize the tf.Session().
Will increasing memory fix this?
Absolutely—this is a classic case of insufficient memory. The core issue is that your system doesn't have enough RAM to hold both the loaded model, TensorFlow's runtime components, and the OS itself.
How much memory do you need?
As a minimum, 2GB of RAM should get you up and running. Here's the breakdown:
- OS + background processes: ~300MB
- TensorFlow 1.4.1 runtime overhead: ~200-300MB
- Loaded model parameters: Likely 300-500MB (based on your 53MB model file)
- Buffer room for temporary tensors during inference: ~100MB
Adding up, 2GB gives you enough headroom to avoid hitting memory limits. For a more stable setup (especially if you might run other small processes alongside), 4GB of RAM would be ideal—it'll eliminate any memory-related bottlenecks and let the model run smoothly.
Quick temporary fixes (if you can't add RAM right away)
If you need a stopgap solution before upgrading memory, try these:
- Shut down all unnecessary background processes on the server to free up RAM (use
toporhtopto identify and kill non-critical ones) - Initialize your session with memory constraints:
config = tf.ConfigProto() # Limit TensorFlow to using only necessary threads (reduces memory overhead) config.intra_op_parallelism_threads = 1 config.inter_op_parallelism_threads = 1 # For GPU setups, enable memory growth to avoid pre-allocating full GPU memory # config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: # Load and run your model - Check if your model has any unnecessary variables saved (e.g., training-only metrics) and re-save a stripped-down version to reduce memory footprint
内容的提问来源于stack exchange,提问作者Sven s

