กระบวนการและวงจรชีวิตของแอป
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ในกรณีส่วนใหญ่ แอปพลิเคชัน Android ทุกแอปจะทำงานในกระบวนการ Linux ของตัวเอง
ระบบจะสร้างกระบวนการนี้สําหรับแอปพลิเคชันเมื่อโค้ดบางส่วนต้องทํางานและยังคงทํางานต่อไปจนกว่าระบบจะต้องเรียกคืนหน่วยความจําเพื่อใช้โดยแอปพลิเคชันอื่นๆ และไม่จำเป็นต้องใช้กระบวนการดังกล่าวอีกต่อไป
ฟีเจอร์พื้นฐานที่ผิดปกติของ Android คือแอปพลิเคชันไม่ได้ควบคุมอายุการใช้งานของกระบวนการแอปพลิเคชันโดยตรง
แต่ระบบจะกำหนดโดยพิจารณาจากส่วนต่างๆ ของแอปพลิเคชันที่ระบบทราบว่ากำลังทำงานอยู่ ความสำคัญของสิ่งเหล่านี้ต่อผู้ใช้ และปริมาณหน่วยความจำโดยรวมที่มีอยู่ในระบบ
นักพัฒนาแอปต้องเข้าใจว่าคอมโพเนนต์ต่างๆ ของแอป (โดยเฉพาะ 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
อย่างน้อย 1 รายการที่ผู้ใช้ไม่เห็นในขณะนี้ (มีการเรียกใช้เมธอด onStop()
แล้วและแสดงผลแล้ว)
ตราบใดที่นักพัฒนาแอปใช้วงจรชีวิตของ Activity
อย่างถูกต้อง เมื่อระบบหยุดกระบวนการดังกล่าว ประสบการณ์ของผู้ใช้เมื่อกลับมาที่แอปนั้นจะไม่ได้รับผลกระทบ ระบบจะกู้คืนสถานะที่บันทึกไว้ก่อนหน้านี้ได้เมื่อกิจกรรมที่เกี่ยวข้องสร้างขึ้นใหม่ในกระบวนการใหม่ โปรดทราบว่าระบบไม่รับประกันว่าจะมีการเรียกใช้ onDestroy()
ในกรณีที่ระบบหยุดกระบวนการ
ดูรายละเอียดเพิ่มเติมได้ที่ Activity
ตั้งแต่ Android 13 เป็นต้นไป กระบวนการของแอปอาจได้รับเวลาดำเนินการแบบจำกัดหรือไม่ได้รับเวลาดำเนินการเลยจนกว่าจะเข้าสู่สถานะวงจรการทำงานที่ใช้งานอยู่อย่างใดอย่างหนึ่งข้างต้น
ระบบจะเก็บกระบวนการที่แคชไว้เป็นรายการ นโยบายการจัดเรียงที่แน่นอนสำหรับรายการนี้คือรายละเอียดการใช้งานของแพลตฟอร์ม โดยทั่วไปแล้ว ระบบจะพยายามเก็บกระบวนการที่มีประโยชน์มากกว่าไว้ก่อน เช่น กระบวนการที่โฮสต์แอปพลิเคชันหน้าแรกของผู้ใช้หรือกิจกรรมล่าสุดที่ผู้ใช้เห็น ไว้ก่อนกระบวนการประเภทอื่นๆ นอกจากนี้ คุณยังใช้นโยบายอื่นๆ สำหรับการสิ้นสุดกระบวนการได้ด้วย เช่น การกำหนดขีดจำกัดสูงสุดของจำนวนกระบวนการที่อนุญาต หรือจำกัดระยะเวลาที่กระบวนการสามารถแคชไว้ได้อย่างต่อเนื่อง
เมื่อตัดสินใจว่าจะจัดประเภทกระบวนการอย่างไร ระบบจะพิจารณาจากระดับที่สำคัญที่สุดที่พบในคอมโพเนนต์ทั้งหมดที่ใช้งานอยู่ในกระบวนการ
ดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีที่คอมโพเนนต์แต่ละรายการเหล่านี้มีส่วนช่วยในวงจรโดยรวมของกระบวนการและแอปพลิเคชันได้ในเอกสารประกอบของ Activity
, Service
และ BroadcastReceiver
นอกจากนี้ ระบบอาจเพิ่มลําดับความสําคัญของกระบวนการตามลําดับความสําคัญของกระบวนการอื่นที่มีต่อกระบวนการนั้น ตัวอย่างเช่น หากกระบวนการ ก. เชื่อมโยงกับ Service
ที่มี Flag Context.BIND_AUTO_CREATE
หรือใช้ ContentProvider
ในกระบวนการ ข. การจัดประเภทของกระบวนการ ข. จะมีความสำคัญอย่างน้อยเท่ากับกระบวนการ ก. เสมอ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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."]]