mem.py

Created by andreanx

Created on August 08, 2020

739 Bytes

Tests and returns the maximum usable heap. By trying to allocate as many long strings as possible. More reliable if run directly after Python initialisation, or after running gc.collect() when available. Add 640 to the result for the heap load of this script. Use : mem()


def size(o):
  t = type(o)
  s = t == str and 49 + len(o)
  if t == int:
    s = 24
    while o:
      s += 4
      o >>= 30
  elif t == list:
    s = 64 + 8*len(o)
    for so in o:
      s += size(so)
  return s

def mem(v=1):
  try:
    l=[]
    try:
      l.append(0)
      l.append(0)
      l.append("")
      l[2] += "x"
      while 1:
        try:
          l[2] += l[2][l[1]:]
        except:
          if l[1] < len(l[2]) - 1:
            l[1] = len(l[2]) - 1
          else:
            raise(Exception)
    except:
      if v:
        print("+", size(l))
      try:
        l[0] += size(l)
      except:
        pass
      try:
        l[0] += mem(v)
      except:
        pass
      return l[0]
  except:
    return 0