Python gc.get_stats() Examples

The following are 12 code examples of gc.get_stats(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gc , or try the search function .
Example #1
Source File: test_gc.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_get_stats(self):
        stats = gc.get_stats()
        self.assertEqual(len(stats), 3)
        for st in stats:
            self.assertIsInstance(st, dict)
            self.assertEqual(set(st),
                             {"collected", "collections", "uncollectable"})
            self.assertGreaterEqual(st["collected"], 0)
            self.assertGreaterEqual(st["collections"], 0)
            self.assertGreaterEqual(st["uncollectable"], 0)
        # Check that collection counts are incremented correctly
        if gc.isenabled():
            self.addCleanup(gc.enable)
            gc.disable()
        old = gc.get_stats()
        gc.collect(0)
        new = gc.get_stats()
        self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
        self.assertEqual(new[1]["collections"], old[1]["collections"])
        self.assertEqual(new[2]["collections"], old[2]["collections"])
        gc.collect(2)
        new = gc.get_stats()
        self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
        self.assertEqual(new[1]["collections"], old[1]["collections"])
        self.assertEqual(new[2]["collections"], old[2]["collections"] + 1) 
Example #2
Source File: test_gc.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_get_stats(self):
        stats = gc.get_stats()
        self.assertEqual(len(stats), 3)
        for st in stats:
            self.assertIsInstance(st, dict)
            self.assertEqual(set(st),
                             {"collected", "collections", "uncollectable"})
            self.assertGreaterEqual(st["collected"], 0)
            self.assertGreaterEqual(st["collections"], 0)
            self.assertGreaterEqual(st["uncollectable"], 0)
        # Check that collection counts are incremented correctly
        if gc.isenabled():
            self.addCleanup(gc.enable)
            gc.disable()
        old = gc.get_stats()
        gc.collect(0)
        new = gc.get_stats()
        self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
        self.assertEqual(new[1]["collections"], old[1]["collections"])
        self.assertEqual(new[2]["collections"], old[2]["collections"])
        gc.collect(2)
        new = gc.get_stats()
        self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
        self.assertEqual(new[1]["collections"], old[1]["collections"])
        self.assertEqual(new[2]["collections"], old[2]["collections"] + 1) 
Example #3
Source File: gc_collector.py    From client_python with Apache License 2.0 6 votes vote down vote up
def collect(self):
        collected = CounterMetricFamily(
            'python_gc_objects_collected',
            'Objects collected during gc',
            labels=['generation'],
        )
        uncollectable = CounterMetricFamily(
            'python_gc_objects_uncollectable',
            'Uncollectable object found during GC',
            labels=['generation'],
        )

        collections = CounterMetricFamily(
            'python_gc_collections',
            'Number of times this generation was collected',
            labels=['generation'],
        )

        for generation, stat in enumerate(gc.get_stats()):
            generation = str(generation)
            collected.add_metric([generation], value=stat['collected'])
            uncollectable.add_metric([generation], value=stat['uncollectable'])
            collections.add_metric([generation], value=stat['collections'])

        return [collected, uncollectable, collections] 
Example #4
Source File: test_gc.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_get_stats(self):
        stats = gc.get_stats()
        self.assertEqual(len(stats), 3)
        for st in stats:
            self.assertIsInstance(st, dict)
            self.assertEqual(set(st),
                             {"collected", "collections", "uncollectable"})
            self.assertGreaterEqual(st["collected"], 0)
            self.assertGreaterEqual(st["collections"], 0)
            self.assertGreaterEqual(st["uncollectable"], 0)
        # Check that collection counts are incremented correctly
        if gc.isenabled():
            self.addCleanup(gc.enable)
            gc.disable()
        old = gc.get_stats()
        gc.collect(0)
        new = gc.get_stats()
        self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
        self.assertEqual(new[1]["collections"], old[1]["collections"])
        self.assertEqual(new[2]["collections"], old[2]["collections"])
        gc.collect(2)
        new = gc.get_stats()
        self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
        self.assertEqual(new[1]["collections"], old[1]["collections"])
        self.assertEqual(new[2]["collections"], old[2]["collections"] + 1) 
Example #5
Source File: gc.py    From async-worker with MIT License 6 votes vote down vote up
def collect(self):
        collected = CounterMetricFamily(
            f"{self.namespace}python_gc_objects_collected",
            "Objects collected during gc",
            labels=["generation"],
        )
        uncollectable = CounterMetricFamily(
            f"{self.namespace}python_gc_objects_uncollectable",
            "Uncollectable object found during GC",
            labels=["generation"],
        )

        collections = CounterMetricFamily(
            f"{self.namespace}python_gc_collections",
            "Number of times this generation was collected",
            labels=["generation"],
        )

        for generation, stat in enumerate(gc.get_stats()):
            generation = str(generation)
            collected.add_metric([generation], value=stat["collected"])
            uncollectable.add_metric([generation], value=stat["uncollectable"])
            collections.add_metric([generation], value=stat["collections"])

        return [collected, uncollectable, collections] 
Example #6
Source File: memusage.py    From autopush with Mozilla Public License 2.0 5 votes vote down vote up
def memusage(do_dump_rpy_heap=True, do_objgraph=True):
    # type: (Optional[bool], Optional[bool]) -> str
    """Returning a str of memory usage stats"""
    def trap_err(func, *args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:  # pragma: nocover
            # include both __str/repr__, sometimes one's useless
            buf.writelines([func.__name__, ': ', repr(e), ': ', str(e)])

    buf = StringIO()
    rusage = trap_err(resource.getrusage, resource.RUSAGE_SELF)
    buf.writelines([repr(rusage), '\n\n'])
    trap_err(pmap_extended, buf)
    trap_err(jemalloc_stats, buf)
    trap_err(glibc_malloc_info, buf)
    if hasattr(gc, 'get_stats'):
        buf.writelines(['\n\n', gc.get_stats(), '\n\n'])
    if do_dump_rpy_heap:
        # dump rpython's heap before objgraph potentially pollutes the
        # heap with its heavy workload
        trap_err(dump_rpy_heap, buf)
    trap_err(get_stats_asmmemmgr, buf)
    buf.write('\n\n')
    if do_objgraph:
        trap_err(objgraph.show_most_common_types, limit=0, file=buf)
    return buf.getvalue() 
Example #7
Source File: meta.py    From gxf with MIT License 5 votes vote down vote up
def run(self, args):

        print("Gxf %s - %s\n" % (gxf.__version__, gxf.__author__))

        gcstats = gc.get_stats()

        headers = ["generation", "collections", "collected", "uncollectable"]
        tbldata = [["gen %d" % i] + [s[h] for h in headers[1:]]
                   for i, s in enumerate(gcstats)]

        print("Garbage collector statistics:\n")
        print(tabulate.tabulate(tbldata, headers=headers)) 
Example #8
Source File: jsonrpc_server.py    From spruned with MIT License 5 votes vote down vote up
def dev_memorysummary(self):
        return {"stats": gc.get_stats()} 
Example #9
Source File: jsonrpc_server.py    From spruned with MIT License 5 votes vote down vote up
def dev_collect(self):
        res = {
            "before": gc.get_stats()
        }
        gc.collect()
        res['after'] = gc.get_stats()
        return res 
Example #10
Source File: gc_collector.py    From client_python with Apache License 2.0 5 votes vote down vote up
def __init__(self, registry=REGISTRY):
        if not hasattr(gc, 'get_stats') or platform.python_implementation() != 'CPython':
            return
        registry.register(self) 
Example #11
Source File: gc.py    From async-worker with MIT License 5 votes vote down vote up
def __init__(
        self, registry: CollectorRegistry, namespace: str = "", gc=gc
    ) -> None:
        if (
            not hasattr(gc, "get_stats")
            or platform.python_implementation() != "CPython"
        ):
            return
        if namespace:
            self.namespace = f"{namespace}_"
        registry.register(self) 
Example #12
Source File: process_reporter.py    From stackimpact-python with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def report(self):
        # CPU
        if not runtime_info.OS_WIN:
            cpu_time = read_cpu_time()
            if cpu_time != None:
                cpu_time_metric = self.report_metric(Metric.TYPE_COUNTER, Metric.CATEGORY_CPU, Metric.NAME_CPU_TIME, Metric.UNIT_NANOSECOND, cpu_time)
                if cpu_time_metric.has_measurement():
                    cpu_usage = (cpu_time_metric.measurement.value / (60 * 1e9)) * 100
                    try:
                        cpu_usage = cpu_usage / multiprocessing.cpu_count()
                    except Exception:
                        pass

                    self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_CPU, Metric.NAME_CPU_USAGE, Metric.UNIT_PERCENT, cpu_usage)


        # Memory
        if not runtime_info.OS_WIN:
            max_rss = read_max_rss()
            if max_rss != None:
                self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_MEMORY, Metric.NAME_MAX_RSS, Metric.UNIT_KILOBYTE, max_rss)

        if runtime_info.OS_LINUX:
            current_rss = read_current_rss()
            if current_rss != None:
                self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_MEMORY, Metric.NAME_CURRENT_RSS, Metric.UNIT_KILOBYTE, current_rss)

            vm_size = read_vm_size()
            if vm_size != None:
                self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_MEMORY, Metric.NAME_VM_SIZE, Metric.UNIT_KILOBYTE, vm_size)


        # GC stats
        gc_count0, gc_count1, gc_count2 = gc.get_count()
        total_gc_count = gc_count0 + gc_count1 + gc_count2
        self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_GC, Metric.NAME_GC_COUNT, Metric.UNIT_NONE, total_gc_count)

        if min_version(3, 4):
            gc_stats = gc.get_stats()
            if gc_stats and gc_stats[0] and gc_stats[1] and gc_stats[2]:
                total_collections = gc_stats[0]['collections'] + gc_stats[1]['collections'] + gc_stats[2]['collections']
                self.report_metric(Metric.TYPE_COUNTER, Metric.CATEGORY_GC, Metric.NAME_GC_COLLECTIONS, Metric.UNIT_NONE, total_collections)

                total_collected = gc_stats[0]['collected'] + gc_stats[1]['collected'] + gc_stats[2]['collected']
                self.report_metric(Metric.TYPE_COUNTER, Metric.CATEGORY_GC, Metric.NAME_GC_COLLECTED, Metric.UNIT_NONE, total_collected)

                total_uncollectable = gc_stats[0]['uncollectable'] + gc_stats[1]['uncollectable'] + gc_stats[2]['uncollectable']
                self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_GC, Metric.NAME_GC_UNCOLLECTABLE, Metric.UNIT_NONE, total_uncollectable)

        # Runtime
        thread_count = threading.active_count()
        self.report_metric(Metric.TYPE_STATE, Metric.CATEGORY_RUNTIME, Metric.NAME_THREAD_COUNT, Metric.UNIT_NONE, thread_count)