# context-\>setBindingDimensions casing gpu memory leak

**URL:** <https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423>\
**Category:** TensorRT\
**Created:** [October 17, 2019, 9:24am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423 "2019-10-17T09:24:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![yfjiaren](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yfjiaren](https://forums.developer.nvidia.com/u/yfjiaren)\
**Post date:** [October 17, 2019, 9:24am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/1 "2019-10-17T09:24:04Z")

</div>

```auto
context->setBindingDimensions

```

Would case gpu memory leak.

---

<div class="post-metadata">

**Author:** ![yfjiaren](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yfjiaren](https://forums.developer.nvidia.com/u/yfjiaren)\
**Post date:** [October 17, 2019, 9:25am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/2 "2019-10-17T09:25:11Z")

</div>

@NVES_R

---

<div class="post-metadata">

**Author:** ![yfjiaren](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yfjiaren](https://forums.developer.nvidia.com/u/yfjiaren)\
**Post date:** [October 17, 2019, 9:28am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/3 "2019-10-17T09:28:20Z")

</div>

```auto
for(int i=0; i < 1000000; ++i) {
    context->setBindingDimensions
    context->enqueueV2(buffers, stream, nullptr);
}

```

Would case gpu memory leak.

---

<div class="post-metadata">

**Author:** ![NVES\_R](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves_r/32/14043_2.png) [@NVES\_R](https://forums.developer.nvidia.com/u/NVES_R)\
**Post date:** [October 18, 2019, 12:55am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/4 "2019-10-18T00:55:57Z")

</div>

Hi,

Can you provide the full .cpp script for this small example?

Thanks,  
NVIDIA Enterprise Support

---

<div class="post-metadata">

**Author:** ![yfjiaren](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yfjiaren](https://forums.developer.nvidia.com/u/yfjiaren)\
**Post date:** [October 18, 2019, 7:07am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/5 "2019-10-18T07:07:52Z")

</div>

You could just use any \*.engine file with dynamic/fixed input size, and use the following code to find that the gpu memory usage is always rising.

```auto
class MyArray {
    public:
        int inp_size{0};
        int out_size{0};
        int inp_bytes{0};
        int out_bytes{0};
        int time_step{0};
        Dims inp_dims{};
        Dims out_dims{};
        vector<float> inp;
        vector<float> out;

        void set_is_dynamic(bool is_dynamic) {
            if(is_dynamic) {
                inp = vector<float>(MAX_INP_SIZE);
                out = vector<float>(MAX_OUT_SIZE);
            }
            else {
                inp = vector<float>(FIX_INP_SIZE);
                out = vector<float>(FIX_OUT_SIZE);
            }
        }

        void setInpDims(const Dims &dims) {
            inp_size = 1;
            for(int i=0; i < dims.nbDims; ++i) {
                inp_size *= dims.d[i];
            }
            inp_dims = dims;
            inp_bytes = inp_size * sizeof(float);
        }

        void setOutDims(const Dims &dims) {
            out_size = 1;
            for(int i=0; i < dims.nbDims; ++i) {
                out_size *= dims.d[i];
            }
            out_dims = dims;
            out_bytes = out_size * sizeof(float);
        }
};

////////////////////////////////////////////////////////////////

class TrtHelper {
    public:
        TrtHelper(string engine_path, bool is_dynamic) {
            runtime = createInferRuntime(gLogger);
            assert(runtime != nullptr);

            stringstream ss;
            ss.seekg(0, ss.beg);
            ifstream cache(engine_path);
            ss << cache.rdbuf();
            cache.close();
            ss.seekg(0, std::ios::end);
            cint size = ss.tellg();
            ss.seekg(0, std::ios::beg);
            void* memory = malloc(size);
            ss.read((char*)memory, size);
            engine = runtime->deserializeCudaEngine(memory, size, nullptr);
            free(memory);
            assert(engine != nullptr);

            context = engine->createExecutionContext();
            assert(context != nullptr);
            if(is_dynamic) { context->setOptimizationProfile(0); }

            CHECK(cudaStreamCreate(&stream));
            cout << "Loaded " << engine_path << " ..." << endl;

            assert(engine->getNbBindings() == 2);
            inpIndex = engine->getBindingIndex(INP_NAME.c_str());
            outIndex = engine->getBindingIndex(OUT_NAME.c_str());

            auto sf = sizeof(float);
            if(is_dynamic) {
                CHECK(cudaMalloc(&buffers[inpIndex], MAX_INP_SIZE*sf));
                CHECK(cudaMalloc(&buffers[outIndex], MAX_OUT_SIZE*sf));
            }
            else {
                CHECK(cudaMalloc(&buffers[inpIndex], FIX_INP_SIZE*sf));
                CHECK(cudaMalloc(&buffers[outIndex], FIX_OUT_SIZE*sf));
            }

            cout << "Created cuda buffers ..." << endl;
        }

        ~TrtHelper() {
            if(context) { context->destroy(); }
            if(engine) { engine->destroy(); }
            if(runtime) { runtime->destroy(); }
            if(stream) { cudaStreamDestroy(stream); }

            if(buffers[0]) { CHECK(cudaFree(buffers[inpIndex])); buffers[0] = nullptr;}
            if(buffers[1]) { CHECK(cudaFree(buffers[outIndex])); buffers[1] = nullptr;}
        }

        void inference(MyArray &myArray) {
            context->setBindingDimensions(inpIndex, myArray.inp_dims);
            myArray.setOutDims(context->getBindingDimensions(outIndex));

            CHECK(cudaMemcpyAsync(buffers[inpIndex], myArray.inp.data(),
                        myArray.inp_bytes, cudaMemcpyHostToDevice, stream));
cout << "Memory debug beg ..." << endl;
for(int i=0; i < 1000000000; ++i) {
context->setBindingDimensions(inpIndex, myArray.inp_dims);
            context->enqueueV2(buffers, stream, nullptr);
cudaStreamSynchronize(stream);
}
cout << "Memory debug end ..." << endl;
            CHECK(cudaMemcpyAsync(myArray.out.data(), buffers[outIndex],
                        myArray.out_bytes, cudaMemcpyDeviceToHost, stream));
            cudaStreamSynchronize(stream);
        }

    private:
        int inpIndex{0};
        int outIndex{0};
        void* buffers[2]{};

        cudaStream_t stream{nullptr};
        IRuntime *runtime{nullptr};
        ICudaEngine *engine{nullptr};
        IExecutionContext *context{nullptr};
};

ImgHelper img(BLOCK_W, max_inp_w, FIX_INP_H, PAD_VALUE);
        for(auto fip: load_files(input_path)) {
            // cout << ip << endl;
            if(!img.load_img(fip, myArray, is_dynamic)) {
                continue;
            }

            trt.inference(myArray);
            string text = ctc.greedy_ctc(myArray);
            cout << "The text of file '" << fip << "' is: " << text << endl;
        }

```

I could not provide the whole code, but I think the above code is enough as an example =\> You could simply take `!img.load_img(fip, myArray, is_dynamic)` as `setting image data to myArray.inp`.

---

<div class="post-metadata">

**Author:** ![yfjiaren](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yfjiaren](https://forums.developer.nvidia.com/u/yfjiaren)\
**Post date:** [October 18, 2019, 7:15am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/6 "2019-10-18T07:15:49Z")

</div>

Case 1:

```auto
for(int i=0; i < 1000000; ++i) {
    context->setBindingDimensions
    context->enqueueV2(buffers, stream, nullptr);
}

```

Case 2:

```auto
for(int i=0; i < 1000000; ++i) {
    context->enqueueV2(buffers, stream, nullptr);
}

```

Case 3:

```auto
for(int i=0; i < 1000000; ++i) {
    context->setBindingDimensions
}

```

Only case 1 would cause memory usage to raise, and case 2 and case 3 is ok.

---

<div class="post-metadata">

**Author:** ![NVES\_R](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves_r/32/14043_2.png) [@NVES\_R](https://forums.developer.nvidia.com/u/NVES_R)\
**Post date:** [November 1, 2019, 8:32pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/7 "2019-11-01T20:32:19Z")

</div>

Just got an answer, they caught this in a similar bug and have fixed it for the next release. Thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![yfjiaren](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yfjiaren](https://forums.developer.nvidia.com/u/yfjiaren)\
**Post date:** [December 3, 2019, 7:36am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/8 "2019-12-03T07:36:32Z")

</div>

Is the fixed version released yet ?

---

<div class="post-metadata">

**Author:** ![NVES\_R](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves_r/32/14043_2.png) [@NVES\_R](https://forums.developer.nvidia.com/u/NVES_R)\
**Post date:** [December 3, 2019, 9:58pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/9 "2019-12-03T21:58:44Z")

</div>

Hi yfjiaren,

Not yet. Sorry, but I can’t release the timeline.

---

<div class="post-metadata">

**Author:** ![nilshinn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nilshinn](https://forums.developer.nvidia.com/u/nilshinn)\
**Post date:** [December 23, 2019, 12:51pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/10 "2019-12-23T12:51:43Z")

</div>

> [@NVES\_R](#):
>
> Hi yfjiaren,
> 
> Not yet. Sorry, but I can’t release the timeline.

> **[TensorRT6 Dynamic Input Size is slow and has gpu memory leak.](https://forums.developer.nvidia.com/default/topic/1065025/tensorrt/tensorrt6-dynamic-input-size-is-slow-and-has-gpu-memory-leak-/)**
>
> context-\>setBindingDimensions The above interface would cause slow performance and gpu memory leak.

Gpu memory leak is fixed on TensorRT7,  
but, slow performance is Same as before.

---

<div class="post-metadata">

**Author:** ![NVES\_R](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves_r/32/14043_2.png) [@NVES\_R](https://forums.developer.nvidia.com/u/NVES_R)\
**Post date:** [December 23, 2019, 9:12pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/11 "2019-12-23T21:12:10Z")

</div>

Hi yfjiaren,

TRT7 has been released and should fix the memory leak issue.

---

<div class="post-metadata">

**Author:** ![flazerain](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@flazerain](https://forums.developer.nvidia.com/u/flazerain)\
**Post date:** [December 24, 2019, 2:48am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/12 "2019-12-24T02:48:05Z")

</div>

> [@NVES\_R](#):
>
> Hi yfjiaren,
> 
> TRT7 has been released and should fix the memory leak issue.

I found tensorRT7 also has this problem.

I use python api and every time I call context.set\_binding\_shape and context.execute\_async\_v2, the gpu memory grows utill out of memory.

But it didn’t happen to one of my other simple network.

---

<div class="post-metadata">

**Author:** ![nilshinn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nilshinn](https://forums.developer.nvidia.com/u/nilshinn)\
**Post date:** [December 24, 2019, 7:18am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/13 "2019-12-24T07:18:56Z")

</div>

> [@NVES\_R](#):
>
> Hi yfjiaren,
> 
> TRT7 has been released and should fix the memory leak issue.

> **[TensorRT6 Dynamic Input Size is slow and has gpu memory leak.](https://forums.developer.nvidia.com/default/topic/1065025/tensorrt/tensorrt6-dynamic-input-size-is-slow-and-has-gpu-memory-leak-/)**
>
> context-\>setBindingDimensions The above interface would cause slow performance and gpu memory leak.

Gpu memory leak is fixed on TensorRT7,  
but, slow performance is Same as before.  
I use P100

---

<div class="post-metadata">

**Author:** ![echosmask](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@echosmask](https://forums.developer.nvidia.com/u/echosmask)\
**Post date:** [December 26, 2019, 1:56am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/14 "2019-12-26T01:56:34Z")

</div>

@NVES_R, @yfjiaren, @nilshinn

> [@](#):
>
> > [@NVES\_R](#):
> >
> > Hi yfjiaren,
> > 
> > TRT7 has been released and should fix the memory leak issue.
> 
> I found tensorRT7 also has this problem.
> 
> I use python api and every time I call context.set\_binding\_shape and context.execute\_async\_v2, the gpu memory grows utill out of memory.
> 
> But it didn’t happen to one of my other simple network.

Indeed, ResNet-like heavy networks built by TRT python api do make GPU memory leak each time when I call _context.set\_binding\_shape_ and _context.execute\_async\_v2_. Thus, TensorRT7.0 still has this problem. Besides, the environment is as follows:

- Tesla P4
- CUDA 10.0.130
- TensorRT 7.0.0.11

* * *

**Update**

Tesla P4 do memory leak. RTX 2080TI do not leak.

---

<div class="post-metadata">

**Author:** ![flazerain](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@flazerain](https://forums.developer.nvidia.com/u/flazerain)\
**Post date:** [January 2, 2020, 7:29am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/15 "2020-01-02T07:29:05Z")

</div>

> [@](#):
>
> > [@NVES\_R](#):
> >
> > Hi yfjiaren,
> > 
> > TRT7 has been released and should fix the memory leak issue.
> 
> I found tensorRT7 also has this problem.
> 
> I use python api and every time I call context.set\_binding\_shape and context.execute\_async\_v2, the gpu memory grows utill out of memory.
> 
> But it didn’t happen to one of my other simple network.

CUDA 10.0.130  
TensorRT 7.0.0.11  
gtx 1060  
retinanet-resnet50

update:  
V100 do not leak but is slow(same as pytorch)

---

<div class="post-metadata">

**Author:** ![NVES\_R](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves_r/32/14043_2.png) [@NVES\_R](https://forums.developer.nvidia.com/u/NVES_R)\
**Post date:** [January 2, 2020, 5:30pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/16 "2020-01-02T17:30:37Z")

</div>

Thanks for the updates everyone, looking into this.

---

<div class="post-metadata">

**Author:** ![NVES\_R](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves_r/32/14043_2.png) [@NVES\_R](https://forums.developer.nvidia.com/u/NVES_R)\
**Post date:** [February 14, 2020, 12:33am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/17 "2020-02-14T00:33:05Z")

</div>

This issue has been fixed upstream and should be included in the next release.

---

<div class="post-metadata">

**Author:** ![fran6co](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/fran6co/32/151366_2.png) [@fran6co](https://forums.developer.nvidia.com/u/fran6co)\
**Post date:** [March 10, 2020, 9:03am UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/18 "2020-03-10T09:03:57Z")

</div>

Could you release a hotfix? This is blocking production code for us. Thank you

---

<div class="post-metadata">

**Author:** ![patrick.beaulieu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@patrick.beaulieu](https://forums.developer.nvidia.com/u/patrick.beaulieu)\
**Post date:** [April 1, 2020, 6:43pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/19 "2020-04-01T18:43:05Z")

</div>

@NVES_R  
When will this fix be released? Can a trt 7 hotfix be released prior to trt8?

We are a big customer unable to upgrade from older tensor-rt in released products due to this issue causing application instability.

---

<div class="post-metadata">

**Author:** ![pierre.massat](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pierre.massat](https://forums.developer.nvidia.com/u/pierre.massat)\
**Post date:** [April 1, 2020, 6:46pm UTC](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423/20 "2020-04-01T18:46:53Z")

</div>

@NVES_R I would love to see that fix release very soon too, we’re having this problem and it’s blocking us to upgrade to TRT7.

[Next page](https://forums.developer.nvidia.com/t/context-setbindingdimensions-casing-gpu-memory-leak/83423.md?page=2)
