Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix memory leak for size-zero ndarray #14365

Merged
merged 4 commits into from
Mar 18, 2019
Merged

Conversation

yuxihu
Copy link
Member

@yuxihu yuxihu commented Mar 8, 2019

Fixes #13951 Fixes #14358

For size-zero ndarray (e.g. mx.nd.array([]), mx.nd.ones(0)), the storage handle size is 0. Currently we only free handles which size is larger than 0. This leads to memory leak for size-zero ndarray.

In this PR, we remove the check on storage handle size which was used to decide if we need to free a storage handle. After relaxing the check, we need to make sure nullptr is not reused in pooled storage manager and the context for aux handle is correctly set for sparse ndarray.

With this PR, the memory leak issues mentioned above are fixed.

@yuxihu yuxihu requested a review from anirudh2290 as a code owner March 8, 2019 04:02
@yuxihu
Copy link
Member Author

yuxihu commented Mar 8, 2019

@mxnet-label-bot update [pr-work-in-progress]

@marcoabreu marcoabreu added pr-awaiting-review PR is waiting for code review pr-work-in-progress PR is still work in progress and removed pr-awaiting-review PR is waiting for code review labels Mar 8, 2019
@vandanavk
Copy link
Contributor

@yuxihu Please add "Fixes #14358" as well, so that #14358 is also closed when this PR is merged.

Copy link
Member

@wkcn wkcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!
LGTM: )

@access2rohit
Copy link
Contributor

@yuxihu Can you look at the failing checks. LGTM

@yuxihu
Copy link
Member Author

yuxihu commented Mar 8, 2019

Yes, I am looking into the test failures.

@lupesko
Copy link
Contributor

lupesko commented Mar 11, 2019

Very nice catch @yuxihu !

@yuxihu yuxihu force-pushed the mem_leak branch 4 times, most recently from 2248647 to 142ddcf Compare March 12, 2019 16:03
@access2rohit
Copy link
Contributor

Left few nit picky comments

@yuxihu
Copy link
Member Author

yuxihu commented Mar 12, 2019

@eric-haibin-lin please help review.

@mxnet-label-bot update [pr-awaiting-review]

@marcoabreu marcoabreu added pr-awaiting-review PR is waiting for code review and removed pr-work-in-progress PR is still work in progress labels Mar 12, 2019
include/mxnet/ndarray.h Outdated Show resolved Hide resolved
@wkcn wkcn added pr-awaiting-merge Review and CI is complete. Ready to Merge and removed pr-awaiting-review PR is waiting for code review labels Mar 13, 2019
Copy link
Member

@eric-haibin-lin eric-haibin-lin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still around size 0 to page size?

include/mxnet/ndarray.h Outdated Show resolved Hide resolved
@yuxihu
Copy link
Member Author

yuxihu commented Mar 14, 2019

Do we still around size 0 to page size?

Yes. I am not sure the reason behind the logic so I do not change it in this PR. We also use aligned alloc in CPU which allocates 16/64 bytes when size is 0.

@yuxihu
Copy link
Member Author

yuxihu commented Mar 15, 2019

@eric-haibin-lin It is ready for final review.

@eric-haibin-lin eric-haibin-lin merged commit 3ab1dec into apache:master Mar 18, 2019
@yuxihu yuxihu deleted the mem_leak branch March 18, 2019 20:44
yuxihu added a commit to yuxihu/incubator-mxnet that referenced this pull request Mar 20, 2019
TaoLv pushed a commit that referenced this pull request Mar 20, 2019
vdantu pushed a commit to vdantu/incubator-mxnet that referenced this pull request Mar 31, 2019
* free memory for size zero storage handle

* skip adding nullptr into GPU memory pool

* set context for aux handle

* set context for aux handle once it is created
vdantu pushed a commit to vdantu/incubator-mxnet that referenced this pull request Mar 31, 2019
ZhennanQin pushed a commit to ZhennanQin/incubator-mxnet that referenced this pull request Apr 3, 2019
* free memory for size zero storage handle

* skip adding nullptr into GPU memory pool

* set context for aux handle

* set context for aux handle once it is created
ZhennanQin pushed a commit to ZhennanQin/incubator-mxnet that referenced this pull request Apr 3, 2019
nswamy pushed a commit that referenced this pull request Apr 5, 2019
* free memory for size zero storage handle

* skip adding nullptr into GPU memory pool

* set context for aux handle

* set context for aux handle once it is created
nswamy pushed a commit that referenced this pull request Apr 5, 2019
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* free memory for size zero storage handle

* skip adding nullptr into GPU memory pool

* set context for aux handle

* set context for aux handle once it is created
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory builds up when creating size-zero NDArray in a loop Gluon RNN memory leaks with extra variables
8 participants