{"action":"created","issue":{"url":"https://api.github.com/repos/torch/torch7/issues/229","repository_url":"https://api.github.com/repos/torch/torch7","labels_url":"https://api.github.com/repos/torch/torch7/issues/229/labels{/name}","comments_url":"https://api.github.com/repos/torch/torch7/issues/229/comments","events_url":"https://api.github.com/repos/torch/torch7/issues/229/events","html_url":"https://github.com/torch/torch7/issues/229","id":74722323,"number":229,"title":"Memory leak when creating Tensors inside a for loop ?","user":{"login":"fmassa","id":9110200,"avatar_url":"https://avatars.githubusercontent.com/u/9110200?v=3","gravatar_id":"","url":"https://api.github.com/users/fmassa","html_url":"https://github.com/fmassa","followers_url":"https://api.github.com/users/fmassa/followers","following_url":"https://api.github.com/users/fmassa/following{/other_user}","gists_url":"https://api.github.com/users/fmassa/gists{/gist_id}","starred_url":"https://api.github.com/users/fmassa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fmassa/subscriptions","organizations_url":"https://api.github.com/users/fmassa/orgs","repos_url":"https://api.github.com/users/fmassa/repos","events_url":"https://api.github.com/users/fmassa/events{/privacy}","received_events_url":"https://api.github.com/users/fmassa/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":16,"created_at":"2015-05-09T17:44:01Z","updated_at":"2016-08-11T08:24:46Z","closed_at":"2015-05-17T23:44:19Z","body":"There might be a memory leak in the following (toy) example:\r\n\r\n```lua\r\nfor i=1,10000 do\r\n  local a = torch.Tensor(10000):zero()\r\nend\r\n```\r\nThis allocates around 600MB of memory, and I can't free it afterwards, even after calling `collectgarbage()` several times. If I run this loop several times, more memory gets used and no way to get it back, unless closing `luajit`. If I add a `collectgarbage()` inside the for loop, everything works fine, but the loop takes 20x more time to complete.\r\n\r\nI understand that torch Tensors are allocated on C side, and that memory is freed when there is no more reference to the underlying data. I thought it could be a `luajit` problem, but using `jit.off()` didn't change anything.\r\n\r\nAny thoughts about that ? Or is the only solution to avoid Tensor creation at all price, reusing already created Tensors in situations like that ?\r\n"},"comment":{"url":"https://api.github.com/repos/torch/torch7/issues/comments/239100410","html_url":"https://github.com/torch/torch7/issues/229#issuecomment-239100410","issue_url":"https://api.github.com/repos/torch/torch7/issues/229","id":239100410,"user":{"login":"CDLuminate","id":5723047,"avatar_url":"https://avatars.githubusercontent.com/u/5723047?v=3","gravatar_id":"","url":"https://api.github.com/users/CDLuminate","html_url":"https://github.com/CDLuminate","followers_url":"https://api.github.com/users/CDLuminate/followers","following_url":"https://api.github.com/users/CDLuminate/following{/other_user}","gists_url":"https://api.github.com/users/CDLuminate/gists{/gist_id}","starred_url":"https://api.github.com/users/CDLuminate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CDLuminate/subscriptions","organizations_url":"https://api.github.com/users/CDLuminate/orgs","repos_url":"https://api.github.com/users/CDLuminate/repos","events_url":"https://api.github.com/users/CDLuminate/events{/privacy}","received_events_url":"https://api.github.com/users/CDLuminate/received_events","type":"User","site_admin":false},"created_at":"2016-08-11T08:24:46Z","updated_at":"2016-08-11T08:24:46Z","body":"@soumith I can't repro this issue with a small script, but now it seems to be solved by saving cloned model.\r\n\r\noriginally I was directly saving the model, e.g.\r\n```lua\r\nlocal model = Model()\r\nfor i = 1, math.huge do\r\n   forward, backward, update\r\n   torch.save(path, model)\r\nend\r\n```\r\nand the memory usage keeps increasing even if `collectgarbage('count')` is frequently called, and the script will end up with `luajit: not enough memory` after several saves. With reference to Andrej's neuraltalk2 code, I now save the cloned model like this\r\n```\r\nlocal model = Model()\r\nlocal thin_model = model:clone()\r\nthin_model.component = model.component:clone()\r\nfor i = 1, math.huge do\r\n   forward, backward, update\r\n   torch.save(path, thin_model)\r\nend\r\n```\r\nand the memory problem seems to be gone. But why?"}}
{"actor":{"display_login":"CDLuminate"}}