Model's memoryUsage double its size after a inference request
Model's memoryUsage double its size after a request. model archive size: 106MB memoryUsage after load: 175-220 MB after an inference request (or a couple of) memoryUsage spike to ~400MB
in a pure python I can control the process size to be less than 200 MB always.

Originally posted by @sermakarevich in https://github.com/pytorch/serve/issues/481#issuecomment-649718275
Please comment in #481 - This requires fix to default handlers where with no_grade: context manager is not used.
Updates - The increase in size [without no_grad] is model dependent per our observations .e.g in case of vgg11 or resnet-18 based cnn models memory increases by 20-30% however for densenet-161, it doubles! Tried running gc explicitly however no memory is released.
NOTE even after deactivating autograd engine, we saw a 10-15% increase in memory footprint based on model type as explained above. Example models provided with torchserve have been taken from torch hub.
Raised ticket with pytorch forum - #89035
Hey @dhaniram-kshirsagar. I have made the fix with no-grad on my end and did additional profiling, this is what I got:
MB
- jit traced model size on disk 150
- torch-model-archive on disk 140
- inside python process (import torch; torch.jit.load) 163 + (50 torch lib), smem PSS
- single with torch.no_grad(): model(torch.rand((1, 3, 400, 400))) >> 451 MB, smem PSS
- inside torchserve process after some requests 465, smem PSS
PID User Command Swap USS PSS RSS
>> python
16346 lilyai python 0 3536 3658 8844
>> import torch
16346 lilyai python 0 45440 47373 99584
>> model = torch.jit.load
>> model.eval()
16346 lilyai python 0 205188 207152 260088
>> with torch.no_grad(): model(torch.rand((1, 3, 400, 400)))
16346 lilyai python 0 433532 435863 495292