ওভারভিউ
এই টিউটোরিয়ালটি দেখাবে কিভাবে কোটলিন (Kotlin) ব্যবহার করে অ্যান্ড্রয়েড অ্যাপ্লিকেশনের হিপ মেমরি সাইজ (Heap Size) প্রোগ্রাম্যাটিক্যালি বের করা যায়। এটি মেমরি ম্যানেজমেন্ট ও পারফরম্যান্স অপ্টিমাইজেশনে সাহায্য করে।
পূর্বশর্ত
- অ্যান্ড্রয়েড স্টুডিও (কোটলিন সাপোর্টসহ)
- বেসিক কোটলিন সিনট্যাক্স জ্ঞান
ধাপ ১: কোটলিন প্রজেক্ট তৈরি করুন
File → New Project → Empty Activity → Language: Kotlin সিলেক্ট করুন।
ধাপ ২: লেআউট (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="getHeapSize"
android:text="Detect Heap Size" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:textColor="@android:color/holo_red_light"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt
package com.example.heapsize
import android.app.ActivityManager
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.Objects
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
}
fun getHeapSize(view: View) {
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val memoryClass: Int = Objects.requireNonNull(activityManager).memoryClass
textView.text = "Heap Size: $memoryClass MB"
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.heapsize">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
চলান ও ফলাফল
"Detect Heap Size" বাটনে ক্লিক করলে টেক্সটভিউতে হিপ সাইজ দেখাবে (যেমন: "Heap Size: 256 MB")।
ব্যাখ্যা
ActivityManager.memoryClass: প্রতি অ্যাপ্লিকেশনের জন্য আলোকিত হিপ মেমরির সর্বোচ্চ সীমা (MB-এ) রিটার্ন করে।- এই মান ডিভাইস এবং OS ভার্সন অনুযায়ী ভিন্ন হয় (সাধারণত 16MB - 512MB+)。
- বड़े হিপের জন্য ম্যানিফেস্টে
android:largeHeap="true"যোগ করা যেতে পারে (সীমিত ব্যবহারে)।
অতিরিক্ত মেমরি তথ্য পেতে
val memoryInfo = ActivityManager.MemoryInfo() activityManager.getMemoryInfo(memoryInfo) val totalMem = Formatter.formatFileSize(this, memoryInfo.totalMem) val availMem = Formatter.formatFileSize(this, memoryInfo.availMem) val threshold = Formatter.formatFileSize(this, memoryInfo.threshold) textView.text = "Total: $totalMem\nAvailable: $availMem\nLow Threshold: $threshold"
সতর্কতা
memoryClass হলো লিমিট, বর্তমান ব্যবহার নয়। বর্তমান মেমরি ইউজেজের জন্য Debug.getNativeHeapAllocatedSize() বা প্রোফাইলার ব্যবহার করুন।