def main():
    """Main function that runs the memory monitoring loop."""
    logging.info("Memory Sentinel started")
    logging.info(f"Memory threshold set to {MEMORY_THRESHOLD_GB}GB")

    try:
        while True:
            available_ram = get_available_ram()
            total_ram = get_total_ram()
            used_ram = get_used_ram()

            logging.debug(
                f"Total RAM: {format_bytes(total_ram)}, "
                + f"Used RAM: {format_bytes(used_ram)}, "
                + f"Available RAM: {format_bytes(available_ram)}"
            )

            if available_ram < MEMORY_THRESHOLD_BYTES:
                logging.warning(
                    f"Available RAM ({format_bytes(available_ram)}) " + f"below threshold of {MEMORY_THRESHOLD_GB}GB"
                )

                process = get_process_with_highest_memory()
                if process:
                    pid, name, memory = process
                    logging.warning(f"Killing process {pid} ({name}) " + f"using {format_bytes(memory)}")

                    if kill_process(pid):
                        logging.info(f"Successfully killed process {pid} ({name})")
                    else:
                        logging.error(f"Failed to kill process {pid} ({name})")
                else:
                    logging.warning("No process found to kill")

            # Sleep for 1 second
            time.sleep(1)

    except KeyboardInterrupt:
        logging.info("Memory Sentinel stopped by user")
    except Exception as e:
        logging.error(f"Unexpected error: {e}")
        raise
Main function that runs the memory monitoring loop.
docs/usage/memory_sentinel.py