Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev version #1374

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}

Loading