sys.getsizeof

Here are the examples of the python api sys.getsizeof taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

198 Examples 7

Example 1

Project: KMCLib
Source File: XYZTrajectory.py
View license
    def _bufferSize(self):
        """
        Calculate and return the buffer size.
        """
        size =  sys.getsizeof(self.__atom_id_coordinates)
        size +=  sys.getsizeof(self.__atom_id_coordinates[0])*len(self.__atom_id_coordinates)
        size += sys.getsizeof(self.__atom_id_types)
        size += sys.getsizeof(self.__atom_id_types[0])*len(self.__atom_id_types)
        size += sys.getsizeof(self.__time)
        size += sys.getsizeof(self.__time[0])*len(self.__time)
        size += sys.getsizeof(self.__step)
        size += sys.getsizeof(self.__step[0])*len(self.__step)

        return size

Example 2

Project: grr
Source File: fake_data_store.py
View license
  def Size(self):
    total_size = sys.getsizeof(self.subjects)
    for subject, record in self.subjects.iteritems():
      total_size += sys.getsizeof(subject)
      total_size += sys.getsizeof(record)
      for attribute, values in record.iteritems():
        total_size += sys.getsizeof(attribute)
        total_size += sys.getsizeof(values)
        for value, timestamp in values:
          total_size += sys.getsizeof(value)
          total_size += sys.getsizeof(timestamp)
    return total_size

Example 3

Project: KMCLib
Source File: XYZTrajectory.py
View license
    def _bufferSize(self):
        """
        Calculate and return the buffer size.
        """
        size =  sys.getsizeof(self.__atom_id_coordinates)
        size +=  sys.getsizeof(self.__atom_id_coordinates[0])*len(self.__atom_id_coordinates)
        size += sys.getsizeof(self.__atom_id_types)
        size += sys.getsizeof(self.__atom_id_types[0])*len(self.__atom_id_types)
        size += sys.getsizeof(self.__time)
        size += sys.getsizeof(self.__time[0])*len(self.__time)
        size += sys.getsizeof(self.__step)
        size += sys.getsizeof(self.__step[0])*len(self.__step)

        return size

Example 4

Project: nocrack
Source File: trie.py
View license
    def __sizeof__(self):
        T = self.Tree
        s = 0;
        for k in T:
            s += getsizeof(T[k]) + sys.getsizeof(k)
        return s;

Example 5

Project: TrustRouter
Source File: test_sys.py
View license
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        # add GC header size
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
              % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 6

Project: pympler
Source File: test_muppy.py
View license
    def test_filter_by_size(self):
        """Test that only elements within the specified size boundaries
        are returned.
        Also verify that if minimum is larger than maximum an exception is
        raised."""
        minimum = 42
        maximum = 958
        objects = []
        for i in range(1000):
            rand = random.randint(0,1000)
            objects.append(' ' * rand)
        objects = muppy.filter(objects, min=minimum, max=maximum)
        for o in objects:
            self.assert_(minimum <= getsizeof(o) <= maximum)

        self.assertRaises(ValueError, muppy.filter, objects, min=17, max=16)

Example 7

Project: bluepass
Source File: perf_skiplist.py
View license
    def perf_memory_overhead(self):
        """Test memory overhead."""
        for logN in range(3, 6):
            items = 10**logN
            sl = self._create_skiplist(items)
            node = sl._head[2]
            extrasize = 0
            while node is not sl._tail:
                extrasize += sys.getsizeof(node)
                if  len(node) > 3:
                    extrasize += sys.getsizeof(node[-1])
                node = node[2]
            overhead = extrasize / len(sl)
            self.add_result(overhead, params={'logN': logN})

Example 8

Project: brython
Source File: __init__.py
View license
    def __sizeof__(self):
        sizeof = _sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 9

Project: pympler
Source File: muppy.py
View license
def get_size(objects):
    """Compute the total size of all elements in objects."""
    res = 0
    for o in objects:
        try:
            res += getsizeof(o)
        except AttributeError:
            print("IGNORING: type=%s; o=%s" % (str(type(o)), str(o)))
    return res

Example 10

Project: pympler
Source File: test_muppy.py
View license
    def test_sort(self):
        """Test that objects are sorted by size."""
        objects = ['', 'a', 'ab', 'ab', 'abc', '0']
        objects = muppy.sort(objects)
        while len(objects) > 1:
            prev_o = objects.pop(0)
            self.assert_(getsizeof(objects[0]) >= getsizeof(prev_o),\
                 "The previous element appears to be larger than the " +\
                 "current: %s<%s" % (prev_o, objects[0]))

Example 11

Project: dpark
Source File: serialize.py
View license
def dump_obj(f, name, obj):
    if obj is f:
        # Prevent infinite recursion when dumping a recursive function
        return dumps(RECURSIVE_FUNCTION_PLACEHOLDER)

    try:
        if sys.getsizeof(obj) > OBJECT_SIZE_LIMIT:
            obj = create_broadcast(name, obj, f.__name__)
    except TypeError:
        pass

    b = dumps(obj)
    if len(b) > OBJECT_SIZE_LIMIT:
        b = dumps(create_broadcast(name, obj, f.__name__))
    if len(b) > OBJECT_SIZE_LIMIT:
        logger.warning("broadcast of %s obj too large", type(obj))
    return b

Example 12

Project: openwrt-mt7620
Source File: test_sys.py
View license
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
                % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 13

Project: pympler
Source File: summary.py
View license
def _subtract(summary, o):
    """Remove object o from the summary by subtracting it's size."""
    found = False
    row = [_repr(o), 1, getsizeof(o)]
    for r in summary:
        if r[0] == row[0]:
            (r[1], r[2]) = (r[1] - row[1], r[2] - row[2])
            found = True
    if not found:
        summary.append([row[0], -row[1], -row[2]])
    return summary

Example 14

Project: TrustRouter
Source File: test_sys.py
View license
    def test_default(self):
        h = self.header
        vh = self.vheader
        size = self.calcsize
        self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
        self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit)

Example 15

Project: itools
Source File: utils.py
View license
def get_sizeof(obj):
    """Return the size of an object and all objects refered by it.
    """
    size = 0
    done = set()
    todo = {id(obj): obj}
    while todo:
        obj_id, obj = todo.popitem()
        size += getsizeof(obj)
        done.add(obj_id)
        done.add(id(obj.__class__)) # Do not count the class
        for obj in get_referents(obj):
            obj_id = id(obj)
            if obj_id not in done:
                todo[obj_id] = obj

    return size

Example 16

Project: nocrack
Source File: trie.py
View license
    def __sizeof__(self):
        T = self.Tree
        s = 0;
        for k in T:
            s += getsizeof(T[k]) + sys.getsizeof(k)
        return s;

Example 17

Project: mysql-tools
Source File: http_handlers.py
View license
def HandlePyHeaps(unused_request, response):
  """Renders /pyheaps page with Python object memory usage."""
  response.headers.add_header('Content-Type', 'text/plain')
  response.start_response(200)

  count = {}
  mem_used = {}
  for obj in gc.get_objects():
    count[type(obj)] = count.get(type(obj), 0) + 1
    mem_used[type(obj)] = mem_used.get(type(obj), 0) + sys.getsizeof(obj)

  response.out.write('Python objects (total bytes, count, type):\n\n')
  for total_size, type_ in sorted(((v, k) for k, v in mem_used.iteritems()),
                                  reverse=True):
    response.out.write('%12s %12s  %s\n' % (total_size, count[type_], type_))

  response.end_response()

Example 18

Project: pymo
Source File: test_sys.py
View license
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
                % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 19

Project: jinjabread
Source File: filehandler.py
View license
def _safe_size(payload, limit):
    ''' if file size limit return false '''
    
    limit = limit * 1024
    size = sys.getsizeof(payload)
    
    if size > limit:
        return False
    return True

Example 20

Project: EmPyre
Source File: helpers.py
View license
def get_file_size(file):
    """
    Returns a string with the file size and highest rating.
    """
    byte_size = sys.getsizeof(file)
    kb_size = byte_size / 1024
    if kb_size == 0:
        byte_size = "%s Bytes" % (byte_size)
        return byte_size
    mb_size = kb_size / 1024
    if mb_size == 0:
        kb_size = "%s KB" % (kb_size)
        return kb_size
    gb_size = mb_size / 1024 % (mb_size)
    if gb_size == 0:
        mb_size = "%s MB" %(mb_size)
        return mb_size
    return "%s GB" % (gb_size)

Example 21

Project: ete
Source File: db.py
View license
def zencode(x, data_id):
    pdata = six.moves.cPickle.dumps(x)
    if sys.getsizeof(pdata) > MAX_SQLITE_SIZE:
        # using protocol 2 fails because of the integer overflow python bug
        # i.e. http://bugs.python.org/issue13555
        six.moves.cPickle.dump(x, open(pjoin(GLOBALS['db_dir'], data_id+".pkl"), "wb"), protocol=1)
        return "__DBDIR__:%s" %data_id
    else:
        return base64.encodestring(zlib.compress(pdata))

Example 22

Project: ironpython3
Source File: __init__.py
View license
    def __sizeof__(self):
        sizeof = _sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 23

Project: dpark
Source File: shuffle.py
View license
    def merge(self, items):
        Merger.merge(self, items)

        self.merged += 1
        if self.max_merge is None:
            current_mem = max(self.get_used_memory() - self.base_memory,
                              sys.getsizeof(self.combined) >> 20)
            if current_mem > self.mem:
                logger.warning(
                    'Too much memory for shuffle, using disk-based shuffle')
                self.max_merge = self.merged

        if self.max_merge is not None and self.merged >= self.max_merge:
            self.rotate()
            self.merged = 0

Example 24

Project: datafari
Source File: test_mmap.py
View license
    @cpython_only
    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
    def test_sizeof(self):
        m1 = mmap.mmap(-1, 100)
        tagname = "foo"
        m2 = mmap.mmap(-1, 100, tagname=tagname)
        self.assertEqual(sys.getsizeof(m2),
                         sys.getsizeof(m1) + len(tagname) + 1)

Example 25

Project: dpark
Source File: table.py
View license
    @table_join
    def leftOuterJoin(self, other, left_keys=None, right_keys=None):
        o = other.indexBy(right_keys).collectAsMap()
        r = self.indexBy(left_keys).map(lambda (k,v):(k,(v,o.get(k))))
        r.mem += (sys.getsizeof(o) * 10) >> 20 # memory used by broadcast obj
        return r

Example 26

Project: Perspectives-Server
Source File: pycache.py
View license
	def __init__(self, key, data, expiry):
		"""Create new cache entry."""

		if (expiry < 1):
			raise ValueError("CacheEntry expiry values must be positive")

		now = int(time.time())

		self.key = key
		self.data = data
		self.expiry = now + expiry
		self.memory_used = sys.getsizeof(data)

		# count the key as having been requested just now, so it is not immediately removed.
		# this is usually correct, as the caller will likely have just retrieved or calculated
		# the data before calling us to store it.
		# this also prevents thrashing so new entries are not rapidly added and then removed from the heap.
		self.last_requested = now

Example 27

Project: tilequeue
Source File: process.py
View license
def _sizeof(val):
    size = 0

    if isinstance(val, dict):
        for k, v in val.items():
            size += len(k) + _sizeof(v)
    elif isinstance(val, list):
        for v in val:
            size += _sizeof(v)
    elif isinstance(val, (str, bytes, unicode)):
        size += len(val)
    else:
        size += getsizeof(val)

    return size

Example 28

Project: openwrt-mt7620
Source File: test_sys.py
View license
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        h = self.header
        size = self.calcsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size(h + 'l'))
        # but lists are
        self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)

Example 29

Project: TrustRouter
Source File: collections.py
View license
    def __sizeof__(self):
        sizeof = _sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 30

Project: openwrt-mt7620
Source File: test_sys.py
View license
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
                % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 31

Project: TrustRouter
Source File: test_sys.py
View license
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        h = self.header
        vh = self.vheader
        size = self.calcsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
        # but lists are
        self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size)

Example 32

Project: python-future
Source File: misc.py
View license
    def __sizeof__(self):
        sizeof = sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 33

Project: INGInious
Source File: tasks_code_boxes.py
View license
    def input_is_consistent(self, taskInput, default_allowed_extension, default_max_size):
        if not BasicBox.input_is_consistent(self, taskInput, default_allowed_extension, default_max_size):
            return False

        try:
            if not taskInput[self.get_complete_id()]["filename"].endswith(tuple(self._allowed_exts or default_allowed_extension)):
                return False

            if sys.getsizeof(taskInput[self.get_complete_id()]["value"]) > (self._max_size or default_max_size):
                return False
        except:
            return False
        return True

Example 34

Project: openwrt-mt7620
Source File: test_sys.py
View license
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        h = self.header
        size = self.calcsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size(h + 'l'))
        # but lists are
        self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)

Example 35

Project: tortik
Source File: __init__.py
View license
    def finish_with_debug(self):
        self.set_header('Content-Type', 'text/html; charset=utf-8')
        if self.debug_type == _DEBUG_ALL:
            self.set_status(200)

        self.finish(RequestHandler.debug_loader.get_template('debug.html').render(
            data=self.log.get_debug_info(),
            output_data=self.get_data(),
            size=sys.getsizeof,
            get_params=lambda x: urlparse.parse_qs(x, keep_blank_values=True),
            pretty_json=lambda x: json.dumps(x, sort_keys=True, indent=4, ensure_ascii=False),
            pretty_xml=lambda x: to_unicode(tostring(x.getroot() if hasattr(x, 'getroot') else x,
                                                     pretty_print=True, encoding='UTF-8')),
            to_unicode=to_unicode,
            dumper=dump,
            format_exception=lambda x: "".join(traceback.format_exception(*x))
        ))

Example 36

Project: cachey
Source File: nbytes.py
View license
def _array(x):
    if x.dtype == 'O':
        return sys.getsizeof('0'*100) * x.size
    elif str(x.dtype) == 'category':
        return _array(x.codes) + _array(x.categories)
    else:
        return x.nbytes

Example 37

Project: pydoop
Source File: pipes.py
View license
    def emit(self, key, value):
        self.used_bytes += sys.getsizeof(key)
        self.used_bytes += sys.getsizeof(value)
        if not self.fast_combiner:
            key = self.__defensive_copy(key)
            value = self.__defensive_copy(value)
        self.data.setdefault(key, []).append(value)
        self.ctx.increment_counter(self.in_rec_counter, 1)
        if self.used_bytes >= self.spill_bytes:
            self.spill_all()

Example 38

Project: pympler
Source File: test_muppy.py
View license
    def test_get_size(self):
        """Test that the return value is the sum of the size of all objects."""
        (o1, o2, o3, o4, o5) = (1, 'a', 'b', 4, 5)
        list = [o1, o2, o3, o4, o5]
        expected = 0
        for o in list:
            expected += getsizeof(o)

        self.assertEqual(muppy.get_size(list), expected)

Example 39

Project: pyhackedit
Source File: misc.py
View license
    def __sizeof__(self):
        sizeof = sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 40

Project: datafari
Source File: test_sys.py
View license
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        size = test.test_support.calcobjsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size('l'))
        # but lists are
        self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)

Example 41

Project: plenum
Source File: util.py
View license
def get_size(obj, seen=None):
    """Recursively finds size of objects"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if isinstance(obj, dict):
        size += sum([get_size(v, seen) for v in obj.values()])
        size += sum([get_size(k, seen) for k in obj.keys()])
    elif hasattr(obj, '__dict__'):
        size += get_size(obj.__dict__, seen)
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum([get_size(i, seen) for i in obj])
    return size

Example 42

Project: brython
Source File: test_sys.py
View license
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        vsize = test.support.calcvobjsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
        # but lists are
        self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size)

Example 43

Project: cadquery-freecad-module
Source File: misc.py
View license
    def __sizeof__(self):
        sizeof = sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 44

View license
    def trim_message(self, message):
        import sys
        trim_length = SCARFACE_DEFAULT_MESSAGE_TRIM_LENGTH
        if hasattr(settings, 'SCARFACE_MESSAGE_TRIM_LENGTH'):
            trim_length = settings.SCARFACE_MESSAGE_TRIM_LENGTH

        if sys.getsizeof(message) > trim_length:
            while sys.getsizeof(message) > trim_length:
                message = message[:-3]
            message += '...'
        return message

Example 45

Project: chipsec
Source File: test_sys.py
View license
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
                % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 46

Project: pympler
Source File: test_summary.py
View license
    def test_summarize(self):
        """Test summarize method. """
        objects = [1, 'a', 'b', 'a', 5, [], {}]
        expected = [[summary._repr(''), 3, 3*getsizeof('a')],\
                    [summary._repr(1), 2, 2*getsizeof(1)],\
                    [summary._repr([]), 1, getsizeof([])],\
                    [summary._repr({}), 1, getsizeof({})]]
        res = summary.summarize(objects)
        for row_e in res:
            self.assert_(row_e in expected)

Example 47

Project: kbengine
Source File: __init__.py
View license
    def __sizeof__(self):
        sizeof = _sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 48

Project: kbengine
Source File: __init__.py
View license
    def __sizeof__(self):
        sizeof = _sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 49

Project: cadquery-freecad-module
Source File: misc.py
View license
    def __sizeof__(self):
        sizeof = sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 50

Project: dask
Source File: cache.py
View license
    def _posttask(self, key, value, dsk, state, id):
        duration = default_timer() - self.starttimes[key]
        deps = state['dependencies'][key]
        if deps:
            duration += max(self.durations.get(k, 0) for k in deps)
        self.durations[key] = duration
        nb = self._nbytes(value) + overhead + sys.getsizeof(key) * 4
        self.cache.put(key, value, cost=duration / nb / 1e9, nbytes=nb)