0

resource.getrusage is showing different results inside docker container?

What I am trying to do:

  • Running small scripts(c/c++/python3/java) inside a container in a file named temp.
  • During the execution calculating the memory used by the script using getrusage(RUSAGE_CHILDREN).ru_maxrss

Solutions I have tried:

https://docs.python.org/3/library/resource.html#resource.RUSAGE_BOTH

Total memory used by Python process?

This is a simplified script I am using to run test.py and ./test inside a temp folder:

main.py


import subprocess
from resource import *
from  datetime import datetime
import json

input_data = b"4\n5"
time = 5

status = True

mem_start = getrusage(RUSAGE_CHILDREN).ru_maxrss
print(mem_start)

try:
    output = subprocess.run(f"cd temp/ && python3 test.py"  ,shell=True , stdout=subprocess.PIPE, stderr=subprocess.PIPE ,  input=input_data , timeout=time)

    result = output.stdout.decode()
    print(result)
    
    mem_end = getrusage(RUSAGE_CHILDREN).ru_maxrss-mem_start   

except :
    status = False
    print("TLE")    
    mem_end = 0
    
status_data = {
    "status" : "Successful" ,
    "memory" : mem_end  
}
print(status_data)

now if I run the below test.py script which allocates theoretical 78125kb of memory through the main.py script it will show 86544kb. Which is pretty close.

test.py

a = input()
b = input()

import sys

# allocating memory
a = [0]*10000000
print(sys.getsizeof(a)/1024)

But running a compiled c++ script it shows 5696kb(locally) and in the container 0kb which is not correct. The same thing happening for compile java script.

c++ script:

#include<iostream>

using namespace std;

int main()
{
    int *p = new int(10000000);
  

    int a , b ;
    scanf("%d %d", &a , &b) ;
    return 0;
}

Am I doing something wrong with python resources module? I have also used RUSAGE_SELF but it does not work.

I am running this script inside a nodejs server.

Moreover is there any other way around for knowing memory usage of a script? Pls Let me know.

Naheed
  • 43
  • 6

0 Answers0