{"record":{"id":"087c62de4c320ed1","repo":"Tencent/matrix","slug":"matrix-is-not-installed-or-memoryinfofactory-is-no","errorCode":null,"errorMessage":"Matrix is NOT installed or MemoryInfoFactory is not initialized!!!","messagePattern":"Matrix is NOT installed or MemoryInfoFactory is not initialized!!!","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/MemInfoFactory.kt","lineNumber":39,"sourceCode":" */\nobject MemInfoFactory {\n    @Volatile\n    @JvmStatic\n    private var manualInitialized = false\n\n    @Volatile\n    @JvmStatic\n    private var application: Application? = null\n\n    @JvmStatic\n    fun init(app: Application) {\n        application = app\n        manualInitialized = true\n    }\n\n    init {\n        if (!manualInitialized && !Matrix.isInstalled()) {\n            throw IllegalStateException(\"Matrix is NOT installed or MemoryInfoFactory is not initialized!!!\")\n        }\n    }\n\n    val activityManager = (if (manualInitialized) application else Matrix.with().application)\n        ?.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager\n\n    val memClass = activityManager.memoryClass\n    val largeMemClass = activityManager.largeMemoryClass\n}\n\ndata class ProcessInfo(\n    val pid: Int = Process.myPid(),\n    val name: String = MatrixUtil.getProcessName(Matrix.with().application),\n    val activity: String = ProcessUILifecycleOwner.recentScene.substringAfterLast('.'),\n    val isProcessFg: Boolean = ProcessUIStartedStateOwner.active(),\n    val isAppFg: Boolean = ProcessSupervisor.isAppUIForeground\n) : Parcelable {\n    constructor(parcel: Parcel) : this(","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/MemInfoFactory.kt#L21-L57","documentation":"MemInfoFactory reads memory stats through an ActivityManager obtained either from a manually provided Application or from Matrix.with().application. Its init block throws IllegalStateException when neither manual initialization happened nor Matrix is installed, because there is no Application context to resolve ActivityManager from.","triggerScenarios":"Instantiating or using MemInfoFactory (or its default instance) when Matrix.with() was never called (Matrix.install not run) and MemInfoFactory.manualInit(app) was not invoked beforehand.","commonSituations":"Using memory-stat utilities in a unit test or a process where Matrix was never installed; calling MemInfoFactory before Matrix.with().install(this) in Application.onCreate; disabling Matrix in debug builds while stats code still runs.","solutions":["Install Matrix first: Matrix.with().install(this) in Application.onCreate before any MemInfoFactory usage.","Alternatively call MemInfoFactory.manualInit(application) if you want memory stats without full Matrix install.","Gate memory-stat collection behind Matrix.isInstalled() checks and skip when not installed.","In non-Matrix contexts, construct the ActivityManager from your own context instead of MemInfoFactory."],"exampleFix":"// before\nval pss = MemInfoFactory.getPss()\n// after\nif (Matrix.isInstalled()) {\n    val pss = MemInfoFactory.getPss()\n} else {\n    MemInfoFactory.manualInit(application)\n    val pss = MemInfoFactory.getPss()\n}","handlingStrategy":"validation","validationCode":"if (!Matrix.isInstalled()) {\n    MemInfoFactory.manualInit(application)\n}\nval pss = MemInfoFactory.getPss()","typeGuard":"fun memInfoReady(): Boolean = Matrix.isInstalled()\nfun getPssSafe(): Long =\n    if (memInfoReady()) MemInfoFactory.getPss() else -1L","tryCatchPattern":"val pss = try {\n    MemInfoFactory.getPss()\n} catch (e: IllegalStateException) {\n    MatrixLog.w(TAG, \"MemInfoFactory not initialized\", e)\n    -1L\n}","preventionTips":["Install Matrix in Application.onCreate before any memory-stat collection.","Use manualInit(application) in processes or tests where Matrix is not installed.","Gate stats collection behind Matrix.isInstalled() in shared utility code."],"tags":["android","memory","initialization-order","matrix-install"],"backgroundTag":"module-init-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}