mem.py

created by andreanx

created on May 25, 2019

793 Bytes

Tests and returns the maximum usable memory. By trying to allocate as many long strings as possible. More reliable if run directly after Python initialisation. Use : mem()


def sizeenv():
  s=0
  import __main__
  for o in dir(__main__):
    try:s+=size(eval(o))
    except:pass
  return s
def size(o):
  s,t=0,type(o)
  if t==str:s=49+len(o)
  if str(t)=="<class 'function'>":s=136
  if t==int:
    s=24
    while o:
      s+=4
      o>>=30
  if t==list:
    s+=64
    for so in o:s+=8+size(so)
  return s
def mem(v=1,r=1):
  try:
    l=[]
    try:
      l+=[r and 793+sizeenv()]
      if v*r:print(" ",l[0])
      l+=[0]
      l+=[""]
      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,0)
      except:pass
      return l[0]
  except:return 0