প্রক্রিয়া এবং অ্যাপ জীবনচক্র
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
বেশিরভাগ ক্ষেত্রে, প্রতিটি অ্যান্ড্রয়েড অ্যাপ্লিকেশন তার নিজস্ব লিনাক্স প্রক্রিয়ায় চলে। এই প্রক্রিয়াটি অ্যাপ্লিকেশনটির জন্য তৈরি করা হয় যখন এর কিছু কোড চালানোর প্রয়োজন হয় এবং যতক্ষণ না সিস্টেমটি অন্যান্য অ্যাপ্লিকেশনগুলির দ্বারা ব্যবহারের জন্য তার মেমরি পুনরুদ্ধার করার প্রয়োজন হয় এবং এটির আর প্রয়োজন হয় না ততক্ষণ পর্যন্ত চলমান থাকে।
অ্যান্ড্রয়েডের একটি অস্বাভাবিক এবং মৌলিক বৈশিষ্ট্য হল যে একটি অ্যাপ্লিকেশন প্রক্রিয়ার জীবনকাল সরাসরি অ্যাপ্লিকেশন দ্বারা নিয়ন্ত্রিত হয় না । পরিবর্তে, এটি সিস্টেম দ্বারা নির্ধারিত হয় অ্যাপ্লিকেশনটির অংশগুলির সংমিশ্রণের মাধ্যমে যা সিস্টেমটি জানে যেগুলি চলছে, এই জিনিসগুলি ব্যবহারকারীর জন্য কতটা গুরুত্বপূর্ণ এবং সিস্টেমে কতটা সামগ্রিক মেমরি উপলব্ধ।
এটি গুরুত্বপূর্ণ যে অ্যাপ্লিকেশন বিকাশকারীরা বুঝতে পারে যে কীভাবে বিভিন্ন অ্যাপ্লিকেশন উপাদানগুলি (বিশেষত Activity
, Service
এবং BroadcastReceiver
) অ্যাপ্লিকেশনটির প্রক্রিয়াটির জীবনকালকে প্রভাবিত করে৷ এই উপাদানগুলি সঠিকভাবে ব্যবহার না করার ফলে সিস্টেমটি গুরুত্বপূর্ণ কাজ করার সময় অ্যাপ্লিকেশনটির প্রক্রিয়াটিকে মেরে ফেলতে পারে।
একটি প্রসেস লাইফসাইকেল বাগের একটি সাধারণ উদাহরণ হল একটি BroadcastReceiver
যেটি একটি থ্রেড শুরু করে যখন এটি তার BroadcastReceiver.onReceive()
পদ্ধতিতে একটি Intent
গ্রহণ করে এবং তারপরে ফাংশন থেকে ফিরে আসে। এটি ফিরে আসার পরে, সিস্টেমটি বিবেচনা করে যে BroadcastReceiver
আর সক্রিয় থাকবে না, এবং এর হোস্টিং প্রক্রিয়াটির আর প্রয়োজন হবে না, যদি না অন্যান্য অ্যাপ্লিকেশন উপাদান এতে সক্রিয় থাকে।
সুতরাং, সিস্টেম মেমরি পুনরুদ্ধার করার জন্য যে কোনও সময় প্রক্রিয়াটিকে মেরে ফেলতে পারে এবং এটি করার ফলে, এটি প্রক্রিয়ায় চলমান থ্রেডটি বন্ধ করে দেয়। এই সমস্যার সমাধান হল সাধারণত BroadcastReceiver
থেকে একটি JobService
নির্ধারণ করা যাতে সিস্টেমটি জানে যে প্রক্রিয়াটিতে সক্রিয় কাজ হচ্ছে।
মেমরি কম থাকলে কোন প্রক্রিয়াগুলিকে মেরে ফেলতে হবে তা নির্ধারণ করতে, অ্যান্ড্রয়েড প্রতিটি প্রক্রিয়াকে তাদের মধ্যে চলমান উপাদান এবং সেই উপাদানগুলির অবস্থার উপর ভিত্তি করে একটি গুরুত্বপূর্ণ অনুক্রমের মধ্যে রাখে। গুরুত্ব অনুসারে, এই প্রক্রিয়ার প্রকারগুলি হল:
- একটি ফোরগ্রাউন্ড প্রক্রিয়া এমন একটি যা ব্যবহারকারী বর্তমানে যা করছে তার জন্য প্রয়োজনীয়। বিভিন্ন অ্যাপ্লিকেশন উপাদানগুলি এর ধারণ প্রক্রিয়াটিকে বিভিন্ন উপায়ে অগ্রভাগে বিবেচনা করতে পারে। একটি প্রক্রিয়া অগ্রভাগে বিবেচনা করা হয় যদি নিম্নলিখিত শর্তগুলির মধ্যে কোনটি থাকে:
সিস্টেমে এই ধরনের কয়েকটি প্রসেস আছে, এবং মেমরি এত কম হলেই শেষ অবলম্বন হিসাবে এগুলিকে মেরে ফেলা হয় যে এমনকি এই প্রক্রিয়াগুলিও চলতে পারে না। সাধারণত, যদি এটি ঘটে তবে ডিভাইসটি একটি মেমরি পেজিং অবস্থায় পৌঁছেছে, তাই ব্যবহারকারী ইন্টারফেসকে প্রতিক্রিয়াশীল রাখতে এই ক্রিয়াটি প্রয়োজন।
- একটি দৃশ্যমান প্রক্রিয়া কাজ করছে যা ব্যবহারকারী বর্তমানে সচেতন, তাই এটিকে হত্যা করা ব্যবহারকারীর অভিজ্ঞতার উপর একটি লক্ষণীয় নেতিবাচক প্রভাব ফেলে। একটি প্রক্রিয়া নিম্নলিখিত অবস্থার মধ্যে দৃশ্যমান বলে মনে করা হয়:
- এটি এমন একটি
Activity
চালাচ্ছে যা অন-স্ক্রীন ব্যবহারকারীর কাছে দৃশ্যমান কিন্তু অগ্রভাগে নয় (এর onPause()
পদ্ধতি বলা হয়েছে)৷ এটি ঘটতে পারে, উদাহরণস্বরূপ, যদি ফোরগ্রাউন্ড Activity
একটি ডায়ালগ হিসাবে প্রদর্শিত হয় যা পূর্ববর্তী Activity
এর পিছনে দেখতে দেয়। - এটির একটি
Service
রয়েছে যা একটি ফোরগ্রাউন্ড পরিষেবা হিসাবে চলছে, Service.startForeground()
এর মাধ্যমে (যা সিস্টেমকে পরিষেবাটিকে এমন কিছু হিসাবে বিবেচনা করতে বলে যা ব্যবহারকারী সচেতন, বা মূলত এটি দৃশ্যমান ছিল)৷ - এটি এমন একটি পরিষেবা হোস্ট করছে যা সিস্টেম একটি নির্দিষ্ট বৈশিষ্ট্যের জন্য ব্যবহার করছে যা ব্যবহারকারী সচেতন, যেমন একটি লাইভ ওয়ালপেপার বা একটি ইনপুট পদ্ধতি পরিষেবা৷
সিস্টেমে চলমান এই প্রক্রিয়াগুলির সংখ্যা ফোরগ্রাউন্ড প্রক্রিয়াগুলির তুলনায় কম আবদ্ধ, তবে এখনও তুলনামূলকভাবে নিয়ন্ত্রিত। এই প্রক্রিয়াগুলি অত্যন্ত গুরুত্বপূর্ণ বলে বিবেচিত হয় এবং সমস্ত অগ্রভাগের প্রক্রিয়াগুলি চলমান রাখার জন্য এটি করার প্রয়োজন না হলে তা হত্যা করা হয় না।
- একটি পরিষেবা প্রক্রিয়া হল একটি
Service
ধারণ করা যা startService()
পদ্ধতিতে শুরু করা হয়েছে। যদিও এই প্রক্রিয়াগুলি সরাসরি ব্যবহারকারীর কাছে দৃশ্যমান নয়, তারা সাধারণত এমন জিনিসগুলি করে যা ব্যবহারকারীর যত্ন নেয় (যেমন ব্যাকগ্রাউন্ড নেটওয়ার্ক ডেটা আপলোড বা ডাউনলোড), তাই সমস্ত অগ্রভাগ ধরে রাখার জন্য পর্যাপ্ত মেমরি না থাকলে সিস্টেম সর্বদা এই জাতীয় প্রক্রিয়াগুলিকে চালু রাখে। এবং দৃশ্যমান প্রক্রিয়া। যে পরিষেবাগুলি দীর্ঘ সময় ধরে চলছে (যেমন 30 মিনিট বা তার বেশি) তাদের প্রক্রিয়াটি ক্যাশ করা তালিকায় নামিয়ে দেওয়ার জন্য গুরুত্বের সাথে হ্রাস করা হতে পারে।
যে প্রসেসগুলিকে দীর্ঘ সময় ধরে চালানোর প্রয়োজন সেগুলি setForeground
দিয়ে তৈরি করা যেতে পারে। যদি এটি একটি পর্যায়ক্রমিক প্রক্রিয়া হয় যার জন্য কার্যকর করার কঠোর সময় প্রয়োজন, তবে এটি AlarmManager
মাধ্যমে নির্ধারিত হতে পারে। আরও তথ্যের জন্য, দীর্ঘমেয়াদী কর্মীদের জন্য সমর্থন পড়ুন। এটি এমন পরিস্থিতি এড়াতে সাহায্য করে যেখানে দীর্ঘ-চলমান পরিষেবাগুলি যেগুলি অতিরিক্ত সংস্থানগুলি ব্যবহার করে, উদাহরণস্বরূপ, মেমরি লিক করে, সিস্টেমটিকে একটি ভাল ব্যবহারকারীর অভিজ্ঞতা প্রদান করতে বাধা দেয়৷
- একটি ক্যাশেড প্রক্রিয়া এমন একটি যা বর্তমানে প্রয়োজন নেই, তাই মেমরির মতো সংস্থান অন্য কোথাও প্রয়োজন হলে সিস্টেমটি প্রয়োজন অনুসারে এটিকে মেরে ফেলতে মুক্ত। একটি সাধারণ আচরণকারী সিস্টেমে, এইগুলি সম্পদ ব্যবস্থাপনায় জড়িত একমাত্র প্রক্রিয়া।
একটি ভাল-চালিত সিস্টেমে একাধিক ক্যাশড প্রসেস সবসময় উপলব্ধ থাকে, অ্যাপ্লিকেশনগুলির মধ্যে দক্ষ স্যুইচিংয়ের জন্য, এবং প্রয়োজন অনুসারে নিয়মিতভাবে ক্যাশে করা অ্যাপগুলিকে মেরে ফেলে। শুধুমাত্র অত্যন্ত জটিল পরিস্থিতিতে সিস্টেমটি এমন একটি বিন্দুতে পৌঁছায় যেখানে সমস্ত ক্যাশে করা প্রক্রিয়াগুলিকে হত্যা করা হয় এবং এটি অবশ্যই পরিষেবা প্রক্রিয়াগুলিকে হত্যা করা শুরু করে।
যেহেতু ক্যাশড প্রসেসগুলি যেকোন সময় সিস্টেম দ্বারা মেরে ফেলা যেতে পারে, তাই ক্যাশে থাকা অবস্থায় অ্যাপগুলির সমস্ত কাজ বন্ধ করা উচিত৷ যদি ব্যবহারকারী-সমালোচনামূলক কাজটি অবশ্যই অ্যাপ দ্বারা সঞ্চালিত হয়, তবে এটি একটি সক্রিয় প্রক্রিয়া অবস্থা থেকে কাজ চালানোর জন্য উপরের APIগুলির একটি ব্যবহার করা উচিত।
ক্যাশেড প্রসেস প্রায়ই এক বা একাধিক Activity
ইন্সট্যান্স ধারণ করে যা বর্তমানে ব্যবহারকারীর কাছে দৃশ্যমান নয় (তাদের onStop()
পদ্ধতি কল করা হয়েছে এবং ফিরে এসেছে)। সিস্টেম যখন এই ধরনের প্রক্রিয়াগুলিকে মেরে ফেলে তখন তারা তাদের Activity
লাইফসাইকেল সঠিকভাবে প্রয়োগ করে, তবে সেই অ্যাপে ফিরে আসার সময় ব্যবহারকারীর অভিজ্ঞতাকে প্রভাবিত করে না। এটি পূর্বে সংরক্ষিত অবস্থা পুনরুদ্ধার করতে পারে যখন সংশ্লিষ্ট কার্যকলাপ একটি নতুন প্রক্রিয়াতে পুনরায় তৈরি হয়। সচেতন থাকুন যে onDestroy()
কল করার গ্যারান্টি দেওয়া হয় না যে ক্ষেত্রে একটি প্রক্রিয়া সিস্টেমের দ্বারা নিহত হয়। আরো বিস্তারিত জানার জন্য, Activity
দেখুন।
অ্যান্ড্রয়েড 13 থেকে শুরু করে, একটি অ্যাপ প্রক্রিয়া সীমিত বা কার্যকর করার সময় পেতে পারে না যতক্ষণ না এটি উপরের একটি সক্রিয় জীবনচক্র অবস্থায় প্রবেশ করে।
ক্যাশে করা প্রক্রিয়াগুলি একটি তালিকায় রাখা হয়। এই তালিকার জন্য সঠিক ক্রম নীতি হল প্ল্যাটফর্মের বাস্তবায়নের বিশদ। সাধারণত, এটি আরও দরকারী প্রক্রিয়াগুলি রাখার চেষ্টা করে, যেমন ব্যবহারকারীর হোম অ্যাপ্লিকেশন হোস্ট করে বা ব্যবহারকারীর দেখা শেষ কার্যকলাপ, অন্যান্য ধরনের প্রক্রিয়ার আগে। প্রক্রিয়াগুলিকে হত্যা করার জন্য অন্যান্য নীতিগুলিও প্রয়োগ করা যেতে পারে, যেমন অনুমোদিত প্রক্রিয়াগুলির সংখ্যার উপর কঠোর সীমা নির্ধারণ করা বা একটি প্রক্রিয়া ক্রমাগত ক্যাশে থাকতে পারে এমন সময় সীমিত করা।
একটি প্রক্রিয়াকে কীভাবে শ্রেণীবদ্ধ করা যায় তা নির্ধারণ করার সময়, সিস্টেমটি তার সিদ্ধান্তকে ভিত্তি করে সবচেয়ে গুরুত্বপূর্ণ স্তরের উপর যেটি বর্তমানে সক্রিয় সমস্ত উপাদানগুলির মধ্যে পাওয়া যায়। এই উপাদানগুলির প্রতিটি কীভাবে একটি প্রক্রিয়া এবং অ্যাপ্লিকেশনের সামগ্রিক জীবনচক্রে অবদান রাখে সে সম্পর্কে আরও বিশদ বিবরণের জন্য Activity
, Service
এবং BroadcastReceiver
ডকুমেন্টেশন দেখুন৷
একটি প্রক্রিয়ার অগ্রাধিকার অন্যান্য নির্ভরতাগুলির উপর ভিত্তি করেও বাড়ানো যেতে পারে যা একটি প্রক্রিয়াতে রয়েছে। উদাহরণস্বরূপ, যদি প্রসেস A Context.BIND_AUTO_CREATE
পতাকা সহ একটি Service
সাথে আবদ্ধ থাকে বা প্রক্রিয়া B-এ একটি ContentProvider
ব্যবহার করে, তাহলে প্রক্রিয়া B-এর শ্রেণীবিভাগ সর্বদা প্রক্রিয়া A-এর মতোই গুরুত্বপূর্ণ।
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 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-29 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."]]