how to limit GPU usage in tensorflow r1.1 with C API
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In TensorFlow r1.x, the runtime often tries to reserve most GPU memory unless you tell it otherwise. When using the C API, the important detail is that GPU limits are configured through serialized session options, which means the low-level C entry point still depends on a ConfigProto payload describing memory behavior.
The Core Settings That Matter
The two settings most people care about are:
- '
allow_growth, which lets TensorFlow allocate GPU memory gradually instead of preallocating almost everything' - '
per_process_gpu_memory_fraction, which caps the fraction of GPU memory the process may reserve'
These settings live inside GPUOptions, which is nested inside ConfigProto.
Pass a Serialized ConfigProto to TF_SetConfig
The C API itself accepts session configuration bytes through TF_SetConfig. In practice, many projects build those bytes from TensorFlow protobuf definitions in C++.
That is the key mechanism: build a valid config, serialize it, and pass the bytes to the C API.
When to Use allow_growth
allow_growth is often the safest first setting in shared environments.
With this enabled, TensorFlow starts small and requests more GPU memory only as needed. This is useful when:
- several processes share one GPU
- you are not sure how much memory the model will actually need
- you want to reduce aggressive startup reservation
It does not give hard isolation, but it prevents the most common "grab everything immediately" behavior.
When to Use a Memory Fraction
If you need a firmer cap, set a per-process fraction.
This tells TensorFlow to reserve at most about 25 percent of visible GPU memory for that process. The right value depends on the model, batch size, and whether other GPU consumers are active.
Be conservative first. If the cap is too low, you may simply turn an overallocation problem into out-of-memory failures during execution.
Understand the C API Limitation
A subtle point is that the pure C API does not give you nice field-by-field builders for ConfigProto. It accepts the serialized config blob. That is why many C-API users still rely on C++ protobuf code or pre-generated configuration bytes at build time.
So even though the runtime entry point is TF_SetConfig, the configuration authoring step is usually easier in C++ than in plain C.
Verify the Effect at Runtime
After applying the configuration, validate the behavior with system tools.
Watch memory usage as the session starts and as the workload runs. If TensorFlow still reserves more memory than expected, double-check that the session options you configured are actually the ones used to create the session.
Common Pitfalls
A common mistake is constructing the ConfigProto correctly but forgetting to call TF_SetConfig before creating the session.
Another is assuming allow_growth and memory fraction should always be combined. Sometimes allow_growth alone is enough, and a strict fraction just adds unnecessary failure risk.
Developers also sometimes call this the "C API" solution while depending on C++ protobuf definitions. That is normal in practice, but it is worth stating explicitly so the build setup is not surprising.
Summary
- TensorFlow r1.x GPU limits are controlled through
ConfigProtosession settings. - The C API consumes those settings through
TF_SetConfigas serialized bytes. - '
allow_growthis useful when you want TensorFlow to allocate GPU memory gradually.' - '
per_process_gpu_memory_fractionis useful when you need a harder cap.' - In practice, many projects generate the config bytes using TensorFlow's C++ protobuf types before passing them into the C API.

