فرآیندها و چرخه عمر برنامه
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
در بیشتر موارد، هر برنامه اندرویدی در فرآیند لینوکس خود اجرا می شود. این فرآیند زمانی برای برنامه ایجاد می شود که برخی از کدهای آن باید اجرا شوند و تا زمانی که سیستم نیاز به بازیابی حافظه خود برای استفاده توسط سایر برنامه ها داشته باشد و دیگر نیازی به آن نباشد در حال اجرا باقی می ماند.
یکی از ویژگیهای غیرعادی و اساسی اندروید این است که طول عمر یک برنامه کاربردی مستقیماً توسط خود برنامه کنترل نمیشود . در عوض، سیستم از طریق ترکیبی از بخشهایی از برنامه که سیستم میداند در حال اجرا هستند، تعیین میکند که این موارد چقدر برای کاربر مهم هستند و چه مقدار حافظه کلی در سیستم موجود است.
این مهم است که توسعه دهندگان برنامه بفهمند که چگونه مؤلفه های مختلف برنامه (به ویژه Activity
، Service
و BroadcastReceiver
) بر طول عمر فرآیند برنامه تأثیر می گذارد. عدم استفاده صحیح از این مؤلفهها میتواند منجر به از بین رفتن فرآیند برنامه در حین انجام کارهای مهم شود.
یک مثال متداول از یک اشکال چرخه حیات فرآیند، BroadcastReceiver
است که وقتی یک Intent
در متد BroadcastReceiver.onReceive()
خود دریافت می کند، یک رشته را شروع می کند و سپس از تابع برمی گردد. پس از بازگشت، سیستم BroadcastReceiver
را دیگر فعال نمی داند و فرآیند میزبانی آن دیگر مورد نیاز نیست، مگر اینکه سایر اجزای برنامه در آن فعال باشند.
بنابراین، سیستم میتواند در هر زمانی فرآیند را برای بازیابی حافظه از بین ببرد و با انجام این کار، نخ ایجاد شده در فرآیند را خاتمه میدهد. راه حل این مشکل معمولاً برنامه ریزی JobService
از BroadcastReceiver
است تا سیستم بداند که کار فعالی در این فرآیند در حال انجام است.
برای تعیین اینکه کدام فرآیندها در صورت کمبود حافظه حذف شوند، Android هر فرآیند را در یک سلسله مراتب اهمیت بر اساس مؤلفه های در حال اجرا در آنها و وضعیت آن مؤلفه ها قرار می دهد. به ترتیب اهمیت، این انواع فرآیند عبارتند از:
- فرآیند پیش زمینه فرآیندی است که برای کاری که کاربر در حال حاضر انجام می دهد مورد نیاز است. اجزای مختلف برنامه می توانند باعث شوند که فرآیند حاوی آن به طرق مختلف پیش زمینه در نظر گرفته شود. اگر یکی از شرایط زیر وجود داشته باشد، یک فرآیند در پیش زمینه در نظر گرفته می شود:
فقط تعداد کمی از این فرآیندها در سیستم وجود دارد، و اینها تنها در صورتی از بین می روند که حافظه آنقدر کم باشد که حتی این فرآیندها نتوانند به کار خود ادامه دهند. به طور کلی، اگر این اتفاق بیفتد، دستگاه به حالت صفحه بندی حافظه رسیده است، بنابراین این عمل برای پاسخگو نگه داشتن رابط کاربری لازم است.
- یک فرآیند قابل مشاهده در حال انجام کاری است که کاربر در حال حاضر از آن آگاه است، بنابراین کشتن آن تأثیر منفی قابل توجهی بر تجربه کاربر دارد. یک فرآیند در شرایط زیر قابل مشاهده در نظر گرفته می شود:
- در حال اجرای یک
Activity
است که روی صفحه برای کاربر قابل مشاهده است اما در پیش زمینه نیست (روش onPause()
آن فراخوانی شده است). این ممکن است رخ دهد، برای مثال، اگر Activity
پیش زمینه به عنوان یک گفتگو نمایش داده شود که اجازه می دهد Activity
قبلی در پشت آن دیده شود. -
Service
دارد که به عنوان یک سرویس پیشزمینه از طریق Service.startForeground()
اجرا میشود (که از سیستم میخواهد سرویس را بهعنوان چیزی که کاربر از آن آگاه است یا اساساً به گونهای که گویی قابل مشاهده است در نظر بگیرد). - میزبان سرویسی است که سیستم از آن برای ویژگی خاصی که کاربر از آن آگاه است استفاده می کند، مانند تصویر زمینه زنده یا سرویس روش ورودی.
تعداد این فرآیندهای در حال اجرا در سیستم نسبت به فرآیندهای پیش زمینه محدودتر است، اما هنوز نسبتاً کنترل شده است. این فرآیندها بسیار مهم در نظر گرفته میشوند و از بین نمیروند، مگر اینکه انجام این کار برای اجرای تمام فرآیندهای پیشزمینه لازم باشد.
- یک فرآیند سرویس ، نگهداری
Service
است که با متد startService()
شروع شده است. اگرچه این فرآیندها مستقیماً برای کاربر قابل مشاهده نیستند، اما معمولاً کارهایی را انجام می دهند که کاربر به آنها اهمیت می دهد (مانند بارگذاری یا دانلود داده های شبکه پس زمینه)، بنابراین سیستم همیشه چنین فرآیندهایی را در حال اجرا نگه می دارد، مگر اینکه حافظه کافی برای حفظ تمام پیش زمینه وجود نداشته باشد. و فرآیندهای قابل مشاهده خدماتی که برای مدت طولانی اجرا شده اند (مانند 30 دقیقه یا بیشتر) ممکن است از نظر اهمیت کاهش یابد تا روند آنها به لیست ذخیره شده کاهش یابد.
فرآیندهایی که نیاز به اجرای طولانی مدت دارند را می توان با setForeground
ایجاد کرد. اگر یک فرآیند دوره ای است که به زمان دقیق اجرا نیاز دارد، می توان آن را از طریق AlarmManager
برنامه ریزی کرد. برای اطلاعات بیشتر، به پشتیبانی از کارگران طولانی مدت مراجعه کنید. این به جلوگیری از موقعیتهایی کمک میکند که سرویسهای طولانیمدت که از منابع بیش از حد استفاده میکنند، به عنوان مثال، با نشت حافظه، مانع از ارائه یک تجربه کاربری خوب توسط سیستم میشوند.
- یک فرآیند ذخیره شده در حافظه پنهان، فرآیندی است که در حال حاضر مورد نیاز نیست، بنابراین سیستم آزاد است تا زمانی که منابعی مانند حافظه در جاهای دیگر مورد نیاز است، آن را در صورت نیاز از بین ببرد. در یک سیستم با رفتار عادی، اینها تنها فرآیندهایی هستند که در مدیریت منابع دخیل هستند.
سیستمی که به خوبی اجرا میشود، چندین فرآیند ذخیرهسازی شده را همیشه در دسترس دارد، برای جابهجایی کارآمد بین برنامهها، و به طور منظم برنامههای ذخیرهشده را در صورت نیاز از بین میبرد. فقط در شرایط بسیار بحرانی سیستم به نقطهای میرسد که تمام فرآیندهای حافظه پنهان کشته میشوند و باید شروع به کشتن فرآیندهای سرویس کند.
از آنجایی که فرآیندهای ذخیره شده در حافظه پنهان می توانند در هر زمان توسط سیستم از بین بروند، برنامه ها باید در حالت ذخیره سازی کار خود را متوقف کنند. اگر برنامه باید کارهای حیاتی کاربر را انجام دهد، باید از یکی از APIهای بالا برای اجرای کار از حالت فرآیند فعال استفاده کند.
فرآیندهای ذخیره شده در حافظه پنهان اغلب دارای یک یا چند نمونه Activity
هستند که در حال حاضر برای کاربر قابل مشاهده نیستند (روش onStop()
آنها فراخوانی شده و برگردانده شده است). به شرطی که چرخه حیات Activity
خود را به درستی در زمانی که سیستم چنین فرآیندهایی را از بین می برد، پیاده سازی کنند، بر تجربه کاربر هنگام بازگشت به آن برنامه تأثیری نخواهد داشت. هنگامی که فعالیت مرتبط در یک فرآیند جدید دوباره ایجاد می شود، می تواند حالت ذخیره شده قبلی را بازیابی کند. توجه داشته باشید که onDestroy()
تضمینی برای فراخوانی در مواردی که یک فرآیند توسط سیستم از بین میرود، وجود ندارد. برای جزئیات بیشتر، به Activity
مراجعه کنید.
با شروع در Android 13، یک فرآیند برنامه ممکن است تا زمانی که وارد یکی از حالتهای چرخه حیات فعال بالا شود، زمان اجرا محدود یا بدون آن را دریافت کند.
فرآیندهای ذخیره شده در حافظه پنهان در یک لیست نگهداری می شوند. خط مشی دقیق سفارش برای این لیست جزییات پیاده سازی پلتفرم است. به طور کلی، سعی میکند فرآیندهای مفیدتری مانند آنهایی که میزبان برنامه خانگی کاربر هستند یا آخرین فعالیتی که کاربر دیده است را قبل از سایر انواع فرآیندها حفظ کند. سیاستهای دیگری برای از بین بردن فرآیندها نیز میتواند اعمال شود، مانند تعیین محدودیتهای سخت برای تعداد فرآیندهای مجاز یا محدود کردن مدت زمانی که یک فرآیند میتواند به طور مداوم در حافظه پنهان بماند.
هنگام تصمیم گیری در مورد نحوه طبقه بندی یک فرآیند، سیستم تصمیم خود را بر اساس مهم ترین سطح موجود در بین تمام اجزای موجود در فرآیند استوار می کند. برای جزئیات بیشتر در مورد چگونگی کمک هر یک از این مؤلفه ها به چرخه عمر کلی یک فرآیند و برنامه، به مستندات Activity
، Service
و BroadcastReceiver
مراجعه کنید.
اولویت یک فرآیند نیز ممکن است بر اساس وابستگی های دیگری که یک فرآیند به آن دارد افزایش یابد. برای مثال، اگر فرآیند A به Service
با پرچم Context.BIND_AUTO_CREATE
متصل شده باشد یا از یک ContentProvider
در فرآیند B استفاده می کند، طبقه بندی فرآیند B همیشه حداقل به اندازه فرآیند A مهم است.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]