Skip to content

Commit

Permalink
dev version (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfursin authored Dec 9, 2024
2 parents c865633 + f69c613 commit 7f66e24
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions cm/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion cm/cmind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions cm/cmind/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 7f66e24

Please sign in to comment.