العمليات وعمر التطبيق
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
في معظم الحالات، يتم تشغيل كل تطبيق Android في عملية Linux خاصة به.
يتم إنشاء هذه العملية للتطبيق عندما يحتاج بعض رمزه إلى
التشغيل ويظلّ قيد التشغيل إلى أن يحتاج النظام إلى استرداد ذاكرته لاستخدامه
من قِبل التطبيقات الأخرى، وعندما لا يعود مطلوبًا.
من الميزات غير المعتادة والأساسية في نظام التشغيل Android أنّه لا يتحكّم التطبيق نفسه مباشرةً في مدّة حياة عملية التطبيق.
بدلاً من ذلك، يحدّد النظام هذه القيمة من خلال مجموعة من أجزاء التطبيق
التي يعرف النظام أنّها قيد التشغيل، ومدى أهمية هذه الأجزاء للمستخدم،
وإجمالي سعة الذاكرة المتاحة في النظام.
من المهم أن يعرف مطوّرو التطبيقات كيفية تأثير مكوّنات التطبيق المختلفة (ولا سيما Activity
وService
وBroadcastReceiver
) في مدّة
عملية التطبيق. يمكن أن يؤدي عدم استخدام هذه المكوّنات بشكلٍ صحيح
إلى إيقاف النظام لمعالجة التطبيق أثناء تنفيذه
عملاً مهمًا.
من الأمثلة الشائعة على أخطاء دورة حياة العملية هو
BroadcastReceiver
الذي يبدأ سلسلة محادثات عند
تلقّي Intent
في BroadcastReceiver.onReceive()
الطريقة ثم يعود من الدالة. بعد عودته، يصنّف النظامBroadcastReceiver
على أنّه لم يعُد نشطًا، وأنّ عملية استضافته لم تعُد مطلوبة، ما لم تكن مكوّنات التطبيق الأخرى نشطة فيه.
وبالتالي، يمكن للنظام إنهاء العملية في أي وقت لاسترداد الذاكرة، وعند إجراء ذلك،
يُنهي النظام سلسلة المهام التي تم إنشاؤها أثناء العملية. عادةً ما يكون حلّ هذه المشكلة هو تحديد موعد JobService
من BroadcastReceiver
حتى يعرف
النظام أنّ هناك عملًا نشطًا في هذه العملية.
لتحديد العمليات التي يجب إغلاقها عند انخفاض سعة الذاكرة، يصنِّف Android
كل عملية في تسلسل هرمي للأهمية استنادًا إلى المكوّنات التي تعمل في
هذه العمليات وحالتها. حسب الأهمية، أنواع العمليات هذه هي:
- العملية التي تعمل في المقدّمة هي عملية مطلوبة للقيام بنشاط المستخدم الحالي. يمكن أن تؤدي مكوّنات التطبيق المختلفة إلى اعتبار العملية التي تحتويها في المقدّمة بطرق مختلفة. تُعتبر العملية قيد الاستخدام في المقدّمة إذا كان أيّ من
الشروط التالية ساريًا:
لا تتوفّر سوى بضع عمليات من هذا النوع في النظام، ولا يتم إغلاقها إلا
كحل أخير إذا كانت الذاكرة منخفضة جدًا لدرجة أنّه لا يمكن حتى لهذه العمليات
مواصلة العمل. بشكل عام، إذا حدث ذلك، يعني ذلك أنّ الجهاز
وصل إلى حالة ذاكرة التخزين المؤقت، لذا يكون هذا الإجراء مطلوبًا للحفاظ على استجابة واجهة
المستخدم.
- تُجري العملية المرئية عملية يدركها المستخدم حاليًا،
لذلك سيؤدّي إغلاقها إلى تأثير سلبي ملحوظ في تجربة المستخدم. تُعدّ العملية
مرئية في الحالات التالية:
- يتم تشغيل
Activity
يظهر للمستخدم على الشاشة ولكن ليس في المقدّمة (تمّت دعوة
onPause()
method
). قد يحدث ذلك
مثلاً إذا تم عرض Activity
في المقدّمة على شكل مربّع حوار
يتيح رؤية Activity
السابق من خلاله.
- يحتوي على
Service
يعمل كخدمة تعمل في المقدّمة،
من خلال Service.startForeground()
(الذي
يطلب من النظام التعامل مع الخدمة على أنّها شيء يعلم به المستخدم، أو
كما لو كانت مرئية).
- يستضيف التطبيق خدمة يستخدمها النظام لميزة معيّنة يعرفها
المستخدم، مثل خلفية حية أو خدمة أسلوب إدخال.
إنّ عدد هذه العمليات التي تعمل في النظام أقل تقييدًا من العمليات التي تعمل في المقدّمة، ولكن لا يزال يتم التحكّم فيها نسبيًا. تُعدّ هذه العمليات
مهمة للغاية ولا يتم إيقافها إلا إذا كان ذلك
مطلوبًا لإبقاء جميع العمليات التي تعمل في المقدّمة قيد التشغيل.
- عملية الخدمة هي عملية تمتلك
Service
تم بدء تشغيلها باستخدام الأسلوب
startService()
. على الرغم من أنّه لا يمكن للمستخدم رؤية هذه العمليات مباشرةً، إلا أنّها تُجري بشكل عام إجراءات تهمّه (مثل تحميل أو تنزيل بيانات الشبكة في الخلفية)، لذلك يحافظ النظام دائمًا على تشغيل هذه العمليات ما لم تكن الذاكرة غير كافية للاحتفاظ بجميع العمليات التي تعمل في المقدّمة والعمليات المرئية.
قد يتم خفض شأن الخدمات التي تعمل منذ فترة طويلة (مثل 30 دقيقة أو أكثر)
لإدراجها في القائمة المخزّنة مؤقتًا.
يمكن إنشاء العمليات التي يجب تنفيذها على مدار فترة طويلة باستخدام
setForeground
.
إذا كانت عملية دورية تتطلّب وقت تنفيذ صارمًا، يمكن
تحديد موعد لها من خلال AlarmManager
.
لمزيد من المعلومات، يُرجى الاطّلاع على مقالة التوافق مع عمليات التشغيل التي تستغرق وقتًا طويلاً.
يساعد ذلك في تجنُّب المواقف التي تمنع فيها الخدمات التي تعمل لفترة طويلة وتستخدم موارد زائدة، مثلاً، من خلال تسرُّب الذاكرة، النظام من تقديم تجربة مستخدم جيدة.
- العملية المخزّنة مؤقتًا هي عملية غير مطلوبة حاليًا، لذا
يمكن للنظام إغلاقها حسب الحاجة عندما تكون هناك حاجة إلى موارد مثل الذاكرة في مكان آخر. في النظام الذي يعمل بشكلٍ طبيعي، هذه هي العمليات الوحيدة المشارِكة في إدارة الموارد.
يتضمّن النظام الذي يعمل بكفاءة عمليات متعددة محفوظة في ذاكرة التخزين المؤقت متاحة دائمًا، وذلك للتبديل بكفاءة بين التطبيقات، ويوقف بانتظام التطبيقات المحفوظة في ذاكرة التخزين المؤقت حسب الحاجة.
لا يصل النظام إلى نقطة يتم فيها إغلاق
جميع العمليات المخزّنة مؤقتًا إلا في الحالات الحرجة جدًا، ويجب أن يبدأ بإغلاق عمليات الخدمات.
بما أنّ النظام يمكنه إنهاء العمليات المخزّنة مؤقتًا في أي وقت، يجب أن تتوقف التطبيقات عن العمل بالكامل
عندما تكون في هذه الحالة. إذا كان على التطبيق تنفيذ عمل مهم للمستخدم،
يجب أن يستخدم إحدى واجهات برمجة التطبيقات المذكورة أعلاه لتنفيذ العمل من حالة عملية نشطة.
غالبًا ما تحتوي العمليات المخزّنة مؤقتًا على مثيل واحد أو أكثر من Activity
غير المرئي حاليًا للمستخدم (تمّت دعوة onStop()
method وعادت).
شرط أن يتم تنفيذ دورة حياة Activity
بشكل صحيح عندما يُغلق النظام
هذه العمليات، لن يؤثر ذلك في تجربة المستخدم عند العودة إلى هذا التطبيق.
يمكنه استعادة الحالة المحفوظة سابقًا عند إعادة إنشاء النشاط المرتبط في
عملية جديدة. يُرجى العِلم أنّه لا يمكن ضمان استدعاء onDestroy()
في حال أوقف النظام إحدى العمليات.
لمزيد من التفاصيل، يُرجى الاطّلاع على Activity
.
بدءًا من Android 13، قد تحصل عملية التطبيق على وقت تنفيذ محدود أو لا تحصل على أي وقت تنفيذ إلى أن تدخل
إحدى حالات دورة الحياة النشطة أعلاه.
يتم الاحتفاظ بالعمليات المخزّنة مؤقتًا في قائمة. إنّ سياسة الترتيب الدقيقة لهذه القائمة
هي من تفاصيل تنفيذ المنصة. بشكل عام، يحاول النظام إبقاء العمليات الأكثر
فائدة، مثل تلك التي تستضيف تطبيق المستخدم الرئيسي أو آخر نشاط شاهده المستخدم،
قبل الأنواع الأخرى من العمليات. يمكن أيضًا تطبيق سياسات أخرى لإيقاف العمليات، مثل ضبط حدود صارمة لعدد العمليات المسموح بها أو الحد من المدّة التي يمكن أن تبقى فيها العملية محفوظة في ذاكرة التخزين المؤقت باستمرار.
عند تحديد كيفية تصنيف عملية معيّنة، يستند النظام إلى المستوى
الأكثر أهمية من بين جميع المكوّنات النشطة حاليًا في العملية.
اطّلِع على مستندات Activity
وService
و
BroadcastReceiver
للحصول على مزيد من التفاصيل حول كيفية مساهمة كلّ من هذه المكوّنات في دورة حياة العملية و
التطبيق بشكل عام.
قد تزيد أيضًا أولوية العملية استنادًا إلى التبعيات الأخرى التي ترتبط بها العملية. على سبيل المثال، إذا كانت العملية "أ" مرتبطة بملف تعريف
Service
باستخدام العلامة
Context.BIND_AUTO_CREATE
أو تستخدم ملف تعريف
ContentProvider
في العملية "ب"، يكون تصنيف العملية "ب"
دائمًا مهمًا على الأقل بقدر تصنيف العملية "أ".
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Processes and app lifecycle\n\nIn most cases, every Android application runs in its own Linux process.\nThis process is created for the application when some of its code needs to\nrun and remains running until the system needs to reclaim its memory for use\nby other applications and it is no longer needed.\n\nAn unusual and fundamental feature of Android is that an application process's\nlifetime *isn't* directly controlled by the application itself.\nInstead, it is determined by the system through a combination of the parts of the application\nthat the system knows are running, how important these things are to the user,\nand how much overall memory is available in the system.\n\nIt is important that\napplication developers understand how different application components\n(in particular [Activity](/reference/android/app/Activity), [Service](/reference/android/app/Service),\nand [BroadcastReceiver](/reference/android/content/BroadcastReceiver)) impact the lifetime\nof the application's process. **Not using these components correctly can\nresult in the system killing the application's process while it is doing\nimportant work.**\n\nA common example of a process lifecycle bug is a\n`BroadcastReceiver` that starts a thread when it\nreceives an `Intent` in its [BroadcastReceiver.onReceive()](/reference/android/content/BroadcastReceiver#onReceive(android.content.Context, android.content.Intent))\nmethod and then returns from the function. Once it returns, the system\nconsiders the `BroadcastReceiver` to no longer be active, and its hosting\nprocess to no longer be needed, unless other application components are active in\nit.\n\n\nSo, the system can kill the process at any time to reclaim memory, and in doing so,\nit terminates the spawned thread running in the process. The solution to this problem\nis typically to schedule a [JobService](/reference/android/app/job/JobService)\nfrom the `BroadcastReceiver` so the\nsystem knows that there is active work occurring in the process.\n\nTo determine which processes to kill when low on memory, Android\nplaces each process into an importance hierarchy based on the components running in\nthem and the state of those components. In order of importance, these process types are:\n\n1. A **foreground process** is one that is required for what the user is currently doing. Various application components can cause its containing process to be considered foreground in different ways. A process is considered to be in the foreground if any of the following conditions hold:\n - It is running an [Activity](/reference/android/app/Activity) at the top of the screen that the user is interacting with (its [onResume()](/reference/android/app/Activity#onResume()) method has been called).\n - It has a [BroadcastReceiver](/reference/android/content/BroadcastReceiver) that is currently running (its [BroadcastReceiver.onReceive()](/reference/android/content/BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)) method is executing).\n - It has a [Service](/reference/android/app/Service) that is currently executing code in one of its callbacks ([Service.onCreate()](/reference/android/app/Service#onCreate()), [Service.onStart()](/reference/android/app/Service#onStart(android.content.Intent, int)), or [Service.onDestroy()](/reference/android/app/Service#onDestroy())).\n2. There are only ever a few such processes in the system, and these are only killed as a last resort if memory is so low that not even these processes can continue to run. Generally, if this happens the device has reached a memory paging state, so this action is required to keep the user interface responsive.\n3. A **visible process** is doing work that the user is currently aware of, so killing it has a noticeable negative impact on the user experience. A process is considered visible in the following conditions:\n - It is running an [Activity](/reference/android/app/Activity) that is visible to the user on-screen but not in the foreground (its [onPause()](/reference/android/app/Activity#onPause()) method has been called). This might occur, for example, if the foreground `Activity` is displayed as a dialog that lets the previous `Activity` be seen behind it.\n - It has a [Service](/reference/android/app/Service) that is running as a foreground service, through [Service.startForeground()](/reference/android/app/Service#startForeground(int, android.app.Notification)) (which asks the system to treat the service as something the user is aware of, or essentially as if it were visible).\n - It is hosting a service that the system is using for a particular feature that the user is aware of, such as a live wallpaper or an input method service.\n\n The number of these processes running in the system is less bounded than foreground\n processes, but still relatively controlled. These processes are\n considered extremely important and aren't killed unless doing so is\n required to keep all foreground processes running.\n4. A **service process** is one holding a [Service](/reference/android/app/Service) that has been started with the [startService()](/reference/android/content/Context#startService(android.content.Intent)) method. Though these processes are not directly visible to the user, they are generally doing things that the user cares about (such as background network data upload or download), so the system always keeps such processes running unless there is not enough memory to retain all foreground and visible processes.\n\n Services that have been running for a long time (such as 30 minutes or more) might be\n demoted in importance to let their process drop to the cached list.\n\n Processes that do need to be run over a long period can be created with\n [setForeground](/reference/kotlin/androidx/work/CoroutineWorker#setForeground(androidx.work.ForegroundInfo)).\n If it is a periodic process that requires strict time of execution, it can be\n scheduled through the [AlarmManager](/reference/android/app/AlarmManager).\n For more information, refer to [Support for long-running workers](/topic/libraries/architecture/workmanager/advanced/long-running).\n This helps avoid situations where long-running services that use excessive resources, for\n example, by leaking memory, prevent the system from delivering a good user experience.\n5. A **cached process** is one that is not currently needed, so the system is free to kill it as needed when resources like memory are needed elsewhere. In a normally behaving system, these are the only processes involved in resource management.\n\n \u003cbr /\u003e\n\n A well-running system has multiple cached processes always available, for efficient\n switching between applications, and regularly kills the cached apps as needed.\n Only in very critical situations does the system get to a point where\n all cached processes are killed and it must start killing service processes.\n\n Since cached processes can be killed by the system at any time, apps should cease all work while\n in the cached state. If user-critical work must be performed by the app,\n it should use one of the above APIs to run work from an active process state.\n\n Cached processes often hold one or more [Activity](/reference/android/app/Activity) instances\n that are not currently visible to the user (their\n [onStop()](/reference/android/app/Activity#onStop()) method has been called and has returned).\n Provided they implement their `Activity` lifecycle correctly when the system\n kills such processes, it doesn't impact the user's experience when returning to that app.\n It can restore the previously saved state when the associated activity recreates in\n a new process. Be aware that [onDestroy()](/guide/components/activities/activity-lifecycle#ondestroy)\n is not guaranteed to be called in the case that a process is killed by the system.\n For more details, see [Activity](/reference/android/app/Activity).\n\n Starting in Android 13, an app process may receive limited or no execution time until it enters\n one of the above active lifecycle states.\n\n Cached processes are kept in a list. The exact ordering policy for this list\n is an implementation detail of the platform. Generally, it tries to keep more\n useful processes, such as those hosting the user's home application or the last activity the user saw,\n before other types of processes. Other policies for killing processes can also\n be applied, like setting hard limits on the number of processes allowed or limiting the amount of\n time a process can stay continually cached.\n\nWhen deciding how to classify a process, the system bases its decision on the most\nimportant level found among all the components currently active in the process.\nSee the [Activity](/reference/android/app/Activity), [Service](/reference/android/app/Service), and\n[BroadcastReceiver](/reference/android/content/BroadcastReceiver) documentation for more detail on how\neach of these components contributes to the overall lifecycle of a process and of\nthe application.\n\nA process's priority might also be increased based on other dependencies\na process has to it. For example, if process A has bound to a\n[Service](/reference/android/app/Service) with\nthe [Context.BIND_AUTO_CREATE](/reference/android/content/Context#BIND_AUTO_CREATE)\nflag or is using a\n[ContentProvider](/reference/android/content/ContentProvider) in process B, then process B's\nclassification is always at least as important as process A's."]]