from typing import Tuple
from typing import Optional
import platform
import re


def _get_used_and_total_memory() -> Tuple[Optional[int], Optional[int]]:
    """
    Get information about the used and total system memory.

    Returns:
        A tuple containing the used and total system memory in bytes.
        (None, None) if the system is not Linux or if the reading fails.
        (None, int) if the used memory could not be determined.
    """
    used = None
    total = None

    if platform.system() == "Linux":
        try:
            with open("/proc/meminfo") as f:
                meminfo = f.read()
        except OSError:
            pass  # Prepare for everything and anything.
        else:
            total_match = re.search(r"MemTotal:\s*(\d+)", meminfo)
            available_match = re.search(r"MemAvailable:\s*(\d+)", meminfo)
            if total_match:
                total = int(total_match.group(1)) * 1024
                if available_match:
                    # If MemAvailable exists MemTotal will also exist.
                    # The reverse is not always true (MemAvailable came in Linux 3.14).
                    available = int(available_match.group(1)) * 1024
                    used = total - available

    return used, total