From 617408fa5b39e65d7a81309dad213b148fd571b4 Mon Sep 17 00:00:00 2001 From: Grigori Fursin Date: Mon, 9 Dec 2024 10:41:02 +0100 Subject: [PATCH 1/2] fixing test version --- cm/cmind/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cm/cmind/__init__.py b/cm/cmind/__init__.py index a8087a912..0ca06d95d 100644 --- a/cm/cmind/__init__.py +++ b/cm/cmind/__init__.py @@ -2,7 +2,7 @@ # # Written by Grigori Fursin -__version__ = "3.5.1" +__version__ = "3.5.1.1" from cmind.core import access from cmind.core import x From 78c34ed6c98781b15180b2d45b24c3022879efa3 Mon Sep 17 00:00:00 2001 From: Grigori Fursin Date: Mon, 9 Dec 2024 22:18:15 +0100 Subject: [PATCH 2/2] added utils.get_memory_use --- cm/CHANGES.md | 1 + cm/cmind/utils.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/cm/CHANGES.md b/cm/CHANGES.md index 698ef7493..460124635 100644 --- a/cm/CHANGES.md +++ b/cm/CHANGES.md @@ -1,4 +1,5 @@ ## V3.5.1.1 + - added utils.get_memory_use - formatted Python modules from the internal repository using autopep8 ## V3.5.1 diff --git a/cm/cmind/utils.py b/cm/cmind/utils.py index 5ba5bbce1..7801fe0be 100644 --- a/cm/cmind/utils.py +++ b/cm/cmind/utils.py @@ -2170,3 +2170,52 @@ def substitute_template(template, variables): except KeyError as e: return f"Error: Missing value for {e.args[0]} in the vars dictionary." +############################################################################## +def get_memory_use(console = False): + + """ + Get memory usage + + Args: + console (bool): if True, print to console + + Returns: + memory_use (int) + memory_use_gb (float) + available_memory (int) + available_memory_gb (float) + total_memory (int) + total_memory_gb (float) + + """ + + import os + import psutil + + pid = os.getpid() + + python_process = psutil.Process(pid) + + memory_use = python_process.memory_info()[0] # in bytes + memory_use_gb = memory_use / (1024 ** 3) + + memory_info = psutil.virtual_memory() + + available_memory = memory_info.available # in bytes + total_memory = memory_info.total # in bytes + + available_memory_gb = available_memory / (1024 ** 3) + total_memory_gb = total_memory / (1024 ** 3) + + if console: + print(f"Total Memory: {total_memory_gb:.2f} GB") + print(f"Available Memory: {available_memory_gb:.2f} GB") + print(f"Used Python Memory: {memory_use_gb:.2f} GB") + + return {'return':0, 'memory_use': memory_use, + 'memory_use_gb': memory_use_gb, + 'available_memory': available_memory, + 'available_memory_gb': available_memory_gb, + 'total_memory': total_memory, + 'total_memory_gb': total_memory_gb} +