Skip to content

Commit 7f66e24

Browse files
authored
dev version (#1374)
2 parents c865633 + f69c613 commit 7f66e24

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

cm/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## V3.5.1.1
2+
- added utils.get_memory_use
23
- formatted Python modules from the internal repository using autopep8
34

45
## V3.5.1

cm/cmind/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Written by Grigori Fursin
44

5-
__version__ = "3.5.1"
5+
__version__ = "3.5.1.1"
66

77
from cmind.core import access
88
from cmind.core import x

cm/cmind/utils.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,3 +2170,52 @@ def substitute_template(template, variables):
21702170
except KeyError as e:
21712171
return f"Error: Missing value for {e.args[0]} in the vars dictionary."
21722172

2173+
##############################################################################
2174+
def get_memory_use(console = False):
2175+
2176+
"""
2177+
Get memory usage
2178+
2179+
Args:
2180+
console (bool): if True, print to console
2181+
2182+
Returns:
2183+
memory_use (int)
2184+
memory_use_gb (float)
2185+
available_memory (int)
2186+
available_memory_gb (float)
2187+
total_memory (int)
2188+
total_memory_gb (float)
2189+
2190+
"""
2191+
2192+
import os
2193+
import psutil
2194+
2195+
pid = os.getpid()
2196+
2197+
python_process = psutil.Process(pid)
2198+
2199+
memory_use = python_process.memory_info()[0] # in bytes
2200+
memory_use_gb = memory_use / (1024 ** 3)
2201+
2202+
memory_info = psutil.virtual_memory()
2203+
2204+
available_memory = memory_info.available # in bytes
2205+
total_memory = memory_info.total # in bytes
2206+
2207+
available_memory_gb = available_memory / (1024 ** 3)
2208+
total_memory_gb = total_memory / (1024 ** 3)
2209+
2210+
if console:
2211+
print(f"Total Memory: {total_memory_gb:.2f} GB")
2212+
print(f"Available Memory: {available_memory_gb:.2f} GB")
2213+
print(f"Used Python Memory: {memory_use_gb:.2f} GB")
2214+
2215+
return {'return':0, 'memory_use': memory_use,
2216+
'memory_use_gb': memory_use_gb,
2217+
'available_memory': available_memory,
2218+
'available_memory_gb': available_memory_gb,
2219+
'total_memory': total_memory,
2220+
'total_memory_gb': total_memory_gb}
2221+

0 commit comments

Comments
 (0)