I have the following code:
initial_memory_usage = psutil.virtual_memory()[1]
prediction = model.predict(img)
final_memory_usage = psutil.virtual_memory()[1]
total_mem_usage = initial_memory_usage - final_memory_usage
print("Memory consumed by the predict module: ", total_mem_usage)
I used the aforementioned code with the expectation to calculate the memory usage by 'model. predict(img)' line. I am sometimes getting results as negative, which is not making sense to me. Since psutil.virtual_memory()[1] is supposed to provide available memory according to the official documentation, the second call of this function should return less amount of available memory because some of the memory is assumed to be used by the function 'model.predict(img)'. But this is not the case I am observing.
Please help me understand what I am doing wrong and/or how to calculate the memory usage for a function/module being called within the program.
I tried the above process from psutil documentation but I am getting below results:
Below are the results I am getting for 8 different images:
Memory consumed by the predict module: 127205376
Memory consumed by the predict module: -24231936
Memory consumed by the predict module: -13058048
Memory consumed by the predict module: -13090816
Memory consumed by the predict module: -8667136
Memory consumed by the predict module: 11010048
Memory consumed by the predict module: -28180480
Memory consumed by the predict module: 20709376