{"id":242,"date":"2026-10-23T07:37:00","date_gmt":"2026-10-23T07:37:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=242"},"modified":"2026-11-06T08:48:17","modified_gmt":"2026-11-06T08:48:17","slug":"android-image-loading-performance-with-coil-jetpack-compose-caching-memory-optimization-lazy-lists","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/android-image-loading-performance-with-coil-jetpack-compose-caching-memory-optimization-lazy-lists\/","title":{"rendered":"Android Image Loading Performance with Coil &#038; Jetpack Compose: Caching, Memory Optimization &#038; Lazy Lists"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Images are an essential part of modern Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From photo galleries and social media feeds to e-commerce product catalogs and messaging apps, images make interfaces visually engaging and informative.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, loading images inefficiently can significantly affect application performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Large images consume memory, repeated downloads waste network bandwidth, and expensive decoding operations can lead to slow scrolling or even application crashes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For image-heavy Android applications, optimizing image loading is just as important as optimizing database queries and network requests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fortunately, modern Android development provides powerful solutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Coil 3<\/strong>, a Kotlin-first image loading library, integrates with Jetpack Compose and supports asynchronous image loading, memory caching, disk caching, request configuration, and image transformations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore practical Android image loading performance techniques using <strong>Kotlin, Coil 3, Jetpack Compose, LazyColumn, and LazyVerticalGrid<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also build a simplified photo gallery application that loads images efficiently while keeping scrolling smooth.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Why Android Image Loading Performance Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Images can be among the most resource-intensive elements of an Android interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a gallery application displaying hundreds of photos.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each image may require:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Reading data from local storage or the network.<\/li>\n\n\n\n<li>Decoding the compressed image.<\/li>\n\n\n\n<li>Allocating bitmap memory.<\/li>\n\n\n\n<li>Resizing or transforming the image.<\/li>\n\n\n\n<li>Uploading image data for rendering.<\/li>\n\n\n\n<li>Displaying the image on screen.<\/li>\n\n\n\n<li>Reusing or releasing resources when no longer needed.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If these operations are not managed carefully, users may experience:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Slow image loading.<\/li>\n\n\n\n<li>Scrolling stutter.<\/li>\n\n\n\n<li>Excessive memory consumption.<\/li>\n\n\n\n<li>Repeated network downloads.<\/li>\n\n\n\n<li>Delayed screen rendering.<\/li>\n\n\n\n<li>Out-of-memory errors.<\/li>\n\n\n\n<li>Increased battery usage.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The goal is to display images at the required quality while minimizing unnecessary decoding, memory allocation, and network transfers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Understand the Android Image Loading Pipeline<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A typical image loading process involves several stages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Image Loading Architecture<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Image URL \/ Local URI\n          |\n          v\n      Coil Request\n          |\n          v\n     Memory Cache\n          |\n     +----+----+\n     |         |\n     v         v\n Cache Hit   Cache Miss\n     |         |\n     |         v\n     |      Disk Cache\n     |         |\n     |     +---+---+\n     |     |       |\n     |     v       v\n     |   Cached   Fetch Data\n     |     |       |\n     |     +---+---+\n     |         |\n     |         v\n     |      Decode\n     |         |\n     |         v\n     |    Resize \/ Transform\n     |         |\n     +----+----+\n          |\n          v\n   Jetpack Compose UI<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a simplified conceptual pipeline. The actual stages and cache behavior depend on the request, data source, decoder, and Coil configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A well-designed image loader avoids repeating expensive operations whenever possible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. What Is Coil?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Coil stands for <strong>Coroutine Image Loader<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a Kotlin-first image loading library designed for Android and Kotlin Multiplatform applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil provides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Asynchronous image loading.<\/li>\n\n\n\n<li>Jetpack Compose integration.<\/li>\n\n\n\n<li>Memory caching.<\/li>\n\n\n\n<li>Disk caching support.<\/li>\n\n\n\n<li>Image resizing.<\/li>\n\n\n\n<li>Image transformations.<\/li>\n\n\n\n<li>Request lifecycle management.<\/li>\n\n\n\n<li>Support for multiple data sources.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Coil 3 uses a modular architecture, allowing developers to include the components needed by their applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Coil with Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Coil provides Compose components that simplify image loading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of manually managing bitmap decoding and asynchronous tasks, developers can use Coil&#8217;s image loading APIs directly within composable functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This reduces boilerplate and makes it easier to build responsive image-heavy interfaces.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Add Coil 3 Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For a Jetpack Compose Android application, add Coil dependencies to your app-level <code>build.gradle.kts<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"io.coil-kt.coil3:coil-compose:&lt;coil-version&gt;\"\n    )\n\n    implementation(\n        \"io.coil-kt.coil3:coil-network-okhttp:&lt;coil-version&gt;\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>&lt;coil-version&gt;<\/code> with a compatible stable Coil 3 version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>coil-compose<\/code> artifact provides Compose integration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>coil-network-okhttp<\/code> artifact adds network image loading through OkHttp.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your application loads only local images, you may not need a network artifact.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Required Internet Permission<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For remote images, ensure your manifest includes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;uses-permission\n    android:name=\"android.permission.INTERNET\" \/&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use HTTPS image URLs whenever possible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Load an Image with AsyncImage<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest way to load an image with Coil and Compose is to use <code>AsyncImage<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun ProductImage(\n    imageUrl: String\n) {\n    AsyncImage(\n        model = imageUrl,\n        contentDescription = \"Product image\",\n        modifier = Modifier\n            .size(120.dp),\n        contentScale = ContentScale.Crop\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AsyncImage<\/code> creates and manages an image request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil loads the image asynchronously and updates the composable when the result becomes available.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It also supports automatic image sizing based on layout constraints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why This Is Better Than Manual Bitmap Loading<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Manually loading full-resolution images into composables can cause excessive allocations and unnecessary work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil provides a structured image loading pipeline with caching and request management.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Understand Bitmap Memory Usage<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Compressed image file size is different from decoded bitmap memory usage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a JPEG image may occupy only a few hundred kilobytes on disk.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, when decoded into an uncompressed bitmap, it can consume several megabytes of memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Calculation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider an image with dimensions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4000 \u00d7 3000 pixels<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With a 4-byte-per-pixel bitmap configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4000 \u00d7 3000 \u00d7 4\n= 48,000,000 bytes<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is approximately <strong>45.8 MiB<\/strong> of pixel memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now consider a smaller thumbnail:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>400 \u00d7 300 pixels<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Its equivalent pixel memory is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>400 \u00d7 300 \u00d7 4\n= 480,000 bytes<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Approximately <strong>0.46 MiB<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Lesson<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A large image displayed in a small thumbnail does not need to be decoded at its original dimensions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Decode images near their actual display size whenever practical.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Actual memory usage depends on bitmap configuration, hardware allocation, and other implementation details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Resize Images Before Displaying Them<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Image resizing can significantly reduce memory usage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a gallery thumbnail displayed at 120 \u00d7 120 dp does not usually need the full-resolution source bitmap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil supports size-aware image requests.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explicit Image Request<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun ThumbnailImage(\n    imageUrl: String\n) {\n    val context = LocalContext.current\n\n    val request = remember(\n        context,\n        imageUrl\n    ) {\n        ImageRequest.Builder(context)\n            .data(imageUrl)\n            .size(300, 300)\n            .build()\n    }\n\n    AsyncImage(\n        model = request,\n        contentDescription = \"Thumbnail\",\n        modifier = Modifier.size(120.dp),\n        contentScale = ContentScale.Crop\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example requests an image around 300 \u00d7 300 pixels.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The exact decoded dimensions can vary depending on the source, decoder, scaling rules, and transformations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important Consideration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Coil&#8217;s <code>AsyncImage<\/code> can already resolve a suitable request size from its layout constraints.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Explicit pixel sizes are useful when you have a specific thumbnail requirement, but they should not be added blindly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember that dp and pixels are different units.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Use Memory Caching<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Memory caching stores reusable image results in memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the same image is requested again, Coil may be able to reuse the cached result instead of fetching and decoding it again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Faster repeat image loading.<\/li>\n\n\n\n<li>Reduced decoding work.<\/li>\n\n\n\n<li>Improved scrolling responsiveness.<\/li>\n\n\n\n<li>Less unnecessary processing.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Memory Cache Flow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Image Requested\n       |\n       v\nMemory Cache Lookup\n       |\n   +---+---+\n   |       |\n   v       v\n  Hit     Miss\n   |       |\n   v       v\nReuse    Load Image\nImage      |\n           v\n       Cache Result<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Coil includes memory caching support through its image loading system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can customize the cache configuration when your application has specific requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. Configure a Custom ImageLoader<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For applications that require centralized image loading behavior, configure a shared <code>ImageLoader<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val imageLoader = ImageLoader.Builder(context)\n    .memoryCache {\n        MemoryCache.Builder()\n            .maxSizePercent(\n                context,\n                0.20\n            )\n            .build()\n    }\n    .diskCache {\n        DiskCache.Builder()\n            .directory(\n                context.cacheDir\n                    .resolve(\"coil_images\")\n            )\n            .maxSizeBytes(\n                100L * 1024L * 1024L\n            )\n            .build()\n    }\n    .build()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is an illustrative Coil 3 configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It sets a memory cache budget relative to application memory and a disk cache limit of approximately 100 MiB.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Recommendations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prefer a shared ImageLoader.<\/li>\n\n\n\n<li>Avoid creating a new ImageLoader for every image.<\/li>\n\n\n\n<li>Use an appropriate cache budget.<\/li>\n\n\n\n<li>Consider low-memory devices.<\/li>\n\n\n\n<li>Verify cache behavior with your data sources.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Coil configuration APIs can vary between versions, so review the documentation for the Coil release you&#8217;re using.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Understand Disk Caching<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Disk caching stores reusable image data on device storage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It can help avoid repeated network transfers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Disk caching can improve:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Repeat loading speed.<\/li>\n\n\n\n<li>Data efficiency.<\/li>\n\n\n\n<li>Offline availability for previously cached images.<\/li>\n\n\n\n<li>Battery efficiency.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, disk cache entries are not guaranteed to remain available forever.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operating system may clear application cache storage, and Coil may evict entries to maintain configured limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important Difference<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Memory cache<\/strong> primarily reduces repeated image decoding and loading work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Disk cache<\/strong> can reduce repeated fetching or decoding work, depending on the data source and cache implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Neither should be treated as permanent storage for important user content.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11. Configure Cache Policies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Coil supports cache policies for controlling how requests interact with supported caches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val request = ImageRequest.Builder(context)\n    .data(imageUrl)\n    .memoryCachePolicy(\n        CachePolicy.ENABLED\n    )\n    .diskCachePolicy(\n        CachePolicy.ENABLED\n    )\n    .build()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These settings allow normal memory and disk cache behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When Should Caching Be Disabled?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Some use cases require additional care:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sensitive temporary images.<\/li>\n\n\n\n<li>Rapidly changing content.<\/li>\n\n\n\n<li>Images that must not persist.<\/li>\n\n\n\n<li>Special authentication requirements.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Do not disable caching globally unless the application&#8217;s requirements justify it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For sensitive content, consider both cache policy and how data may be stored by the networking layer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">12. Avoid Repeated ImageLoader Creation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creating a new ImageLoader inside each composable is inefficient.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun BadImageExample(\n    imageUrl: String\n) {\n    val context = LocalContext.current\n\n    val loader = ImageLoader.Builder(context)\n        .build()\n\n    \/\/ New loader created during recomposition.\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A new loader may have separate caches and other resources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use Coil&#8217;s shared image loading infrastructure or configure one application-scoped ImageLoader.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This helps images reuse cache entries and loading resources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For applications using dependency injection, the shared ImageLoader can be provided through the application dependency graph.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">13. Use LazyColumn for Image Lists<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A common mistake is displaying a large list of images in a non-lazy layout.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a Column containing hundreds of image composables can create unnecessary composition and layout work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose provides <code>LazyColumn<\/code> to efficiently display scrolling lists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun PhotoList(\n    photos: List&lt;Photo&gt;\n) {\n    LazyColumn {\n        items(\n            items = photos,\n            key = { it.id }\n        ) { photo -&gt;\n\n            AsyncImage(\n                model = photo.imageUrl,\n                contentDescription = photo.title,\n                modifier = Modifier\n                    .fillMaxWidth()\n                    .height(200.dp),\n                contentScale = ContentScale.Crop\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">LazyColumn composes and lays out items as needed rather than eagerly composing an entire long list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduced unnecessary UI work.<\/li>\n\n\n\n<li>More efficient long lists.<\/li>\n\n\n\n<li>Better scrolling behavior.<\/li>\n\n\n\n<li>Improved memory usage.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Stable keys also help Compose preserve item identity as the dataset changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">14. Use LazyVerticalGrid for Photo Galleries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For photo gallery applications, a grid layout is often more suitable than a vertical list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose provides <code>LazyVerticalGrid<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun PhotoGrid(\n    photos: List&lt;Photo&gt;\n) {\n    LazyVerticalGrid(\n        columns = GridCells.Fixed(3),\n        contentPadding = PaddingValues(4.dp)\n    ) {\n        items(\n            items = photos,\n            key = { it.id }\n        ) { photo -&gt;\n\n            AsyncImage(\n                model = photo.imageUrl,\n                contentDescription = photo.title,\n                modifier = Modifier\n                    .padding(2.dp)\n                    .aspectRatio(1f),\n                contentScale = ContentScale.Crop\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates a three-column photo grid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each item is displayed in a square area.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Performance Tip<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For a photo grid, request thumbnail-sized images instead of repeatedly decoding full-resolution photos.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">15. Use Stable Keys in Lazy Lists<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stable item keys help Compose identify list items across dataset changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without Stable Keys<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>items(photos) { photo -&gt;\n    PhotoItem(photo)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">With Stable Keys<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>items(\n    items = photos,\n    key = { it.id }\n) { photo -&gt;\n    PhotoItem(photo)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Stable keys are particularly useful when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Images are inserted.<\/li>\n\n\n\n<li>Images are deleted.<\/li>\n\n\n\n<li>Items are reordered.<\/li>\n\n\n\n<li>Lists are refreshed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">They help preserve item identity and associated state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keys should be unique and stable for the displayed dataset.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">16. Avoid Unnecessary Image Transformations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Image transformations can include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Blur.<\/li>\n\n\n\n<li>Rounded corners.<\/li>\n\n\n\n<li>Color filters.<\/li>\n\n\n\n<li>Cropping.<\/li>\n\n\n\n<li>Grayscale.<\/li>\n\n\n\n<li>Custom effects.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Some transformations require additional processing and may produce separate cache variants.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val request = ImageRequest.Builder(context)\n    .data(imageUrl)\n    .transformations(\n        CircleCropTransformation()\n    )\n    .build()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example applies a circular crop transformation using the relevant Coil transformation API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Compose UI decorations, consider whether a composable-level modifier is sufficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AsyncImage(\n    model = imageUrl,\n    contentDescription = null,\n    modifier = Modifier\n        .size(80.dp)\n        .clip(CircleShape),\n    contentScale = ContentScale.Crop\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important Distinction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Clipping at the Compose layer and transforming the underlying bitmap are not identical operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Choose based on visual requirements, reuse patterns, and measured performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid repeatedly applying expensive bitmap transformations when a simpler rendering solution meets the design requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">17. Display Loading and Error States<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Image loading may take time or fail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A polished Android application should handle these states gracefully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil provides <code>SubcomposeAsyncImage<\/code> for cases where you need to customize loading states through subcomposition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun ImageWithStates(\n    imageUrl: String\n) {\n    SubcomposeAsyncImage(\n        model = imageUrl,\n        contentDescription = null,\n        modifier = Modifier.size(160.dp),\n        loading = {\n            CircularProgressIndicator()\n        },\n        error = {\n            Icon(\n                imageVector =\n                    Icons.Default.BrokenImage,\n                contentDescription =\n                    \"Image unavailable\"\n            )\n        }\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Performance Consideration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Subcomposition can introduce additional overhead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For performance-sensitive LazyColumn and LazyVerticalGrid screens, prefer <code>AsyncImage<\/code> with suitable placeholder and error painters when possible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>SubcomposeAsyncImage<\/code> when its flexibility is necessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">18. Avoid Large Placeholder Assets<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Placeholders improve perceived loading behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, using a large high-resolution placeholder for every image can waste memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better Placeholder Strategies<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Small vector icons.<\/li>\n\n\n\n<li>Simple colored backgrounds.<\/li>\n\n\n\n<li>Lightweight placeholder drawables.<\/li>\n\n\n\n<li>Low-resolution preview images.<\/li>\n\n\n\n<li>Consistent aspect ratios.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For a photo gallery, a subtle background placeholder is often sufficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The placeholder should not become more expensive than the image loading process itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">19. Use Thumbnail Images for Gallery Screens<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A photo gallery may display hundreds or thousands of images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Loading the original full-resolution image for every grid item is unnecessary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Strategy<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use two image representations:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Thumbnail<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Used in the gallery grid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Full-resolution image<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Loaded when the user opens the image viewer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Architecture<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Photo Library\n     |\n     +-- Thumbnail Request\n     |         |\n     |         v\n     |     Gallery Grid\n     |\n     +-- Full Image Request\n               |\n               v\n          Image Viewer<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Faster grid loading.<\/li>\n\n\n\n<li>Reduced memory consumption.<\/li>\n\n\n\n<li>Lower decoding cost.<\/li>\n\n\n\n<li>Smoother scrolling.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For device media, Android&#8217;s MediaStore and supported thumbnail-loading APIs can help obtain appropriately sized previews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">20. Load Local Images Using MediaStore URIs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For a gallery application, images may come from local device storage instead of a remote server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A MediaStore image can be represented using a content URI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>content:\/\/media\/external\/images\/media\/12345<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Coil can load supported local content URIs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Compose Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun LocalPhoto(\n    photoUri: Uri\n) {\n    AsyncImage(\n        model = photoUri,\n        contentDescription = \"Local photo\",\n        modifier = Modifier\n            .size(120.dp),\n        contentScale = ContentScale.Crop\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important Permission Considerations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Access to shared media depends on Android version, the selected media access approach, and user permissions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For broad gallery functionality, follow current Android media permission requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For user-selected media, consider the Android Photo Picker when it fits the application&#8217;s use case.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not request broad storage permissions when they are unnecessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">21. Avoid Excessive Image Prefetching<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prefetching means loading images before they become visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It can improve perceived responsiveness when used carefully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, aggressive prefetching can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increase network usage.<\/li>\n\n\n\n<li>Consume memory.<\/li>\n\n\n\n<li>Compete with visible image requests.<\/li>\n\n\n\n<li>Waste resources on images the user never views.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prioritize visible content.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prefetch only a small number of likely next images when measurements show a benefit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, an image viewer may preload the next photo in a sequence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A large gallery should not attempt to decode thousands of images in advance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">22. Optimize Image Formats<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing an appropriate image format can reduce download size and storage usage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Formats<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Format<\/th><th>Recommended Use<\/th><\/tr><tr><td>JPEG<\/td><td>Photographic images<\/td><\/tr><tr><td>PNG<\/td><td>Lossless images and transparency<\/td><\/tr><tr><td>WebP<\/td><td>Efficient compressed images<\/td><\/tr><tr><td>AVIF<\/td><td>Supported modern image delivery<\/td><\/tr><tr><td>Vector Drawable<\/td><td>Simple scalable icons<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Recommendations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For remote images:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Serve appropriately sized images.<\/li>\n\n\n\n<li>Use efficient compression.<\/li>\n\n\n\n<li>Avoid unnecessary original-resolution downloads.<\/li>\n\n\n\n<li>Consider modern formats based on device compatibility.<\/li>\n\n\n\n<li>Maintain acceptable visual quality.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For local gallery images, avoid unnecessarily re-encoding user content merely to display it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use thumbnail decoding and caching instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">23. Use Server-Side Image Resizing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For remote images, server-side resizing can provide significant benefits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose an e-commerce API returns a 4000 \u00d7 3000 image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But the Android product grid displays the image at only 120 \u00d7 120 dp.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Downloading the original image wastes bandwidth.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better API Design<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An image service might support:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;images.example.com\/product\/123?w=300<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is an illustrative endpoint.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The server can return a smaller image suitable for the device.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduced download size.<\/li>\n\n\n\n<li>Faster image loading.<\/li>\n\n\n\n<li>Lower memory requirements.<\/li>\n\n\n\n<li>Less decoding work.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For applications that control their backend, responsive image delivery is an important optimization opportunity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">24. Reduce Recomposition-Related Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose recomposition is a normal part of UI rendering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, unnecessary object creation during recomposition can add overhead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid Rebuilding Complex Requests Unnecessarily<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun OptimizedImage(\n    imageUrl: String\n) {\n    val context = LocalContext.current\n\n    val request = remember(\n        context,\n        imageUrl\n    ) {\n        ImageRequest.Builder(context)\n            .data(imageUrl)\n            .build()\n    }\n\n    AsyncImage(\n        model = request,\n        contentDescription = null\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This preserves the request object while its remembered inputs remain unchanged.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not every <code>ImageRequest<\/code> needs to be wrapped in <code>remember<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil and Compose already provide mechanisms for managing image requests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Optimize request creation only when profiling indicates meaningful overhead or when request construction includes expensive work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid unnecessary complexity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">25. Use Immutable UI Models<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For image-heavy lists, predictable UI models help reduce unnecessary work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class Photo(\n    val id: Long,\n    val imageUrl: String,\n    val title: String\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For Compose, prefer models whose fields do not change unexpectedly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the underlying dataset changes, create updated state rather than mutating collections in place without notifying Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use stable item identifiers.<\/li>\n\n\n\n<li>Avoid unnecessary list reconstruction.<\/li>\n\n\n\n<li>Keep expensive transformations outside composable rendering.<\/li>\n\n\n\n<li>Use ViewModel state holders.<\/li>\n\n\n\n<li>Use immutable or consistently managed UI state.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These practices make image-heavy interfaces easier to reason about and optimize.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">26. Measure Image Loading Performance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Performance optimization should be based on measurements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Useful metrics include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Time until the first image appears.<\/li>\n\n\n\n<li>Image decode duration.<\/li>\n\n\n\n<li>Memory consumption.<\/li>\n\n\n\n<li>Cache hit behavior.<\/li>\n\n\n\n<li>Network transfer size.<\/li>\n\n\n\n<li>Scroll frame timing.<\/li>\n\n\n\n<li>Garbage collection frequency.<\/li>\n\n\n\n<li>Image loading failure rate.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Android Studio Profiler<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Android Studio provides tools for inspecting application memory and performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use them to investigate:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Large bitmap allocations.<\/li>\n\n\n\n<li>Excessive garbage collection.<\/li>\n\n\n\n<li>Memory growth during scrolling.<\/li>\n\n\n\n<li>Unexpected retained objects.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Coil EventListener<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Coil provides event-listener APIs that can help observe image request activity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Depending on the Coil version, listeners can report stages such as request start, fetch, decode, and completion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the APIs documented for your specific Coil 3 release.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid logging sensitive image URLs or authentication information in production telemetry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">27. Practical Mini Project: High-Performance Photo Gallery<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build a simplified photo gallery using Kotlin, Coil, and Jetpack Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Project Features<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Our gallery will:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Display a grid of photos.<\/li>\n\n\n\n<li>Load images asynchronously.<\/li>\n\n\n\n<li>Use stable item keys.<\/li>\n\n\n\n<li>Display square thumbnails.<\/li>\n\n\n\n<li>Open a full-screen image viewer.<\/li>\n\n\n\n<li>Reuse Coil&#8217;s caching infrastructure.<\/li>\n\n\n\n<li>Avoid loading all full-resolution images simultaneously.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Architecture<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Photo Gallery Screen\n         |\n         v\nPhotoViewModel\n         |\n         v\nPhotoRepository\n         |\n         v\nPhoto Metadata\n         |\n         v\nLazyVerticalGrid\n         |\n         v\nCoil ImageLoader\n         |\n         +-- Memory Cache\n         |\n         +-- Disk Cache\n         |\n         +-- Decode \/ Resize<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Photo Model<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class Photo(\n    val id: Long,\n    val imageUrl: String,\n    val title: String\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This model represents the image metadata needed by the UI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class PhotoRepository {\n\n    fun getPhotos(): List&lt;Photo&gt; {\n        return listOf(\n            Photo(\n                id = 1,\n                imageUrl =\n                    \"https:\/\/example.com\/photo1.jpg\",\n                title = \"Mountain\"\n            ),\n            Photo(\n                id = 2,\n                imageUrl =\n                    \"https:\/\/example.com\/photo2.jpg\",\n                title = \"Beach\"\n            ),\n            Photo(\n                id = 3,\n                imageUrl =\n                    \"https:\/\/example.com\/photo3.jpg\",\n                title = \"Forest\"\n            )\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The URLs are placeholders.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a production application, the repository may load data from a REST API, Room database, or MediaStore.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create the ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class PhotoViewModel(\n    private val repository: PhotoRepository\n) : ViewModel() {\n\n    private val _photos =\n        MutableStateFlow(\n            repository.getPhotos()\n        )\n\n    val photos =\n        _photos.asStateFlow()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel exposes photo metadata through StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For large datasets, use Paging 3 rather than loading all metadata into a single list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create a Reusable Thumbnail Composable<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun PhotoThumbnail(\n    photo: Photo,\n    onClick: () -&gt; Unit\n) {\n    AsyncImage(\n        model = photo.imageUrl,\n        contentDescription = photo.title,\n        modifier = Modifier\n            .padding(2.dp)\n            .aspectRatio(1f)\n            .clip(\n                RoundedCornerShape(8.dp)\n            )\n            .clickable(\n                onClick = onClick\n            ),\n        contentScale = ContentScale.Crop\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This composable displays a square image thumbnail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coil handles asynchronous image loading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Build the Gallery Grid<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun PhotoGalleryScreen(\n    viewModel: PhotoViewModel,\n    onPhotoClick: (Photo) -&gt; Unit\n) {\n    val photos by viewModel.photos\n        .collectAsStateWithLifecycle()\n\n    LazyVerticalGrid(\n        columns = GridCells.Fixed(3),\n        contentPadding = PaddingValues(4.dp)\n    ) {\n        items(\n            items = photos,\n            key = { it.id }\n        ) { photo -&gt;\n            PhotoThumbnail(\n                photo = photo,\n                onClick = {\n                    onPhotoClick(photo)\n                }\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This grid uses stable keys and lazy composition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only the required items are actively composed and laid out.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Create the Full-Screen Image Viewer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun FullScreenPhotoViewer(\n    photo: Photo,\n    onBack: () -&gt; Unit\n) {\n    Box(\n        modifier = Modifier\n            .fillMaxSize()\n            .background(Color.Black)\n    ) {\n        AsyncImage(\n            model = photo.imageUrl,\n            contentDescription = photo.title,\n            modifier = Modifier.fillMaxSize(),\n            contentScale = ContentScale.Fit\n        )\n\n        IconButton(\n            onClick = onBack,\n            modifier = Modifier\n                .align(Alignment.TopStart)\n                .padding(16.dp)\n        ) {\n            Icon(\n                imageVector =\n                    Icons.AutoMirrored.Filled.ArrowBack,\n                contentDescription = \"Back\",\n                tint = Color.White\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The viewer loads the selected image in a larger display area.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production applications, consider zoom gestures, orientation changes, system insets, and loading\/error states.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: Test with a Large Dataset<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test the gallery with representative image counts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>100 images\n500 images\n1,000 images\n5,000 images\n10,000 images<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Measure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initial grid rendering time.<\/li>\n\n\n\n<li>Scrolling smoothness.<\/li>\n\n\n\n<li>Memory usage.<\/li>\n\n\n\n<li>Image loading failures.<\/li>\n\n\n\n<li>Cache reuse.<\/li>\n\n\n\n<li>Time to open a full-screen image.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For very large libraries, optimize metadata retrieval and pagination in addition to image loading.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">28. Common Android Image Loading Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Loading Full-Resolution Images for Thumbnails<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This wastes memory and decoding resources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Creating Multiple ImageLoaders<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Separate loaders can reduce cache reuse and increase resource overhead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Using Non-Lazy Layouts for Large Image Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This can create unnecessary composition and layout work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Ignoring Stable Keys<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unstable item identity can cause unnecessary UI work when lists change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Applying Expensive Transformations Everywhere<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Repeated transformations can increase processing cost.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Using Heavy Loading Placeholders<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Large placeholders can consume memory unnecessarily.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Prefetching Too Many Images<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Aggressive prefetching can compete with visible content and waste resources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Ignoring Cache Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Inappropriate caching strategies can cause repeated downloads or excessive storage usage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Downloading Original Images from the Server<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Remote thumbnail delivery can significantly reduce transferred data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Optimizing Without Profiling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use real measurements to identify the largest bottlenecks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">29. Android Image Loading Performance Checklist<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before releasing an image-heavy Android application, review the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a suitable image loading library.<\/li>\n\n\n\n<li>Reuse a shared ImageLoader.<\/li>\n\n\n\n<li>Enable appropriate memory caching.<\/li>\n\n\n\n<li>Configure disk caching where needed.<\/li>\n\n\n\n<li>Decode images near display dimensions.<\/li>\n\n\n\n<li>Use thumbnails for gallery grids.<\/li>\n\n\n\n<li>Use LazyColumn or LazyVerticalGrid.<\/li>\n\n\n\n<li>Provide stable item keys.<\/li>\n\n\n\n<li>Avoid unnecessary bitmap transformations.<\/li>\n\n\n\n<li>Use lightweight placeholders.<\/li>\n\n\n\n<li>Handle image loading errors.<\/li>\n\n\n\n<li>Avoid aggressive prefetching.<\/li>\n\n\n\n<li>Serve optimized remote image sizes.<\/li>\n\n\n\n<li>Choose suitable image formats.<\/li>\n\n\n\n<li>Avoid expensive work during recomposition.<\/li>\n\n\n\n<li>Use lifecycle-aware state collection.<\/li>\n\n\n\n<li>Profile bitmap memory usage.<\/li>\n\n\n\n<li>Test on low-memory devices.<\/li>\n\n\n\n<li>Measure scrolling performance.<\/li>\n\n\n\n<li>Test with large image collections.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">30. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Coil in Android?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Coil is a Kotlin-first image loading library that supports asynchronous loading, caching, decoding, and integration with Jetpack Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Coil suitable for Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Coil provides Compose-specific APIs such as AsyncImage and other image loading components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Coil support memory caching?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Coil supports memory caching to help reuse previously loaded image results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Coil support disk caching?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Coil supports disk caching through its image loading infrastructure, depending on configuration and data source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can I prevent OutOfMemoryError when loading images?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid decoding unnecessarily large images, use appropriately sized thumbnails, reuse image loading infrastructure, and profile memory usage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is LazyVerticalGrid suitable for photo galleries?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. LazyVerticalGrid is a good choice for displaying photo thumbnails in a grid layout.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use full-resolution images in a gallery grid?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, no. Use appropriately sized thumbnails for grid items and load higher-resolution images when users open the image viewer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Coil automatically resize images?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">AsyncImage can determine an appropriate request size from layout constraints. Exact behavior depends on the request and image source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I improve scrolling performance in an image-heavy Compose screen?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use lazy layouts, stable keys, appropriately sized images, caching, lightweight placeholders, and performance profiling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Android image loading performance is essential for building smooth, responsive, and memory-efficient applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining <strong>Coil 3, Kotlin, Jetpack Compose, memory caching, disk caching, thumbnail optimization, and lazy layouts<\/strong>, developers can significantly reduce unnecessary image processing and improve the user experience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most important principle is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Load images at the size you actually need, reuse cached results, and avoid unnecessary work while users scroll.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These techniques are particularly valuable for photo galleries, e-commerce applications, social media feeds, and other image-heavy Android experiences.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With a well-designed image loading architecture, Android applications can display rich visual content while maintaining responsive interfaces and efficient resource usage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Official References<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coil-kt.github.io\/coil\/\" target=\"_blank\" rel=\"noopener\">Coil Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coil-kt.github.io\/coil\/compose\/\" target=\"_blank\" rel=\"noopener\">Coil Compose Integration<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coil-kt.github.io\/coil\/image_loaders\/\" target=\"_blank\" rel=\"noopener\">Coil Image Loaders<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/develop\/ui\/compose\/lists\" target=\"_blank\" rel=\"noopener\">Jetpack Compose Lazy Lists and Grids<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/topic\/performance\/graphics\/manage-memory\" target=\"_blank\" rel=\"noopener\">Android Bitmap Memory Management<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/topic\/performance\/graphics\/load-bitmap\" target=\"_blank\" rel=\"noopener\">Android Image Loading<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/studio\/profile\/memory-profiler\" target=\"_blank\" rel=\"noopener\">Android Studio Memory Profiler<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Published by Moiks Developer Blog \u2014 Practical Android development tutorials using Kotlin, Jetpack Compose, and modern Android architecture.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to optimize Android image loading using Coil 3, Kotlin, and Jetpack Compose. Explore memory and disk caching, image resizing, thumbnails, LazyColumn, LazyVerticalGrid, and efficient image loading techniques to reduce memory usage, improve scrolling performance, and build faster Android applications.<\/p>\n","protected":false},"author":1,"featured_media":243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[13,3,333,6],"tags":[66,37,65,67,28,26,34,69,68],"class_list":["post-242","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-performance","category-android-development","category-android-tutorials","category-jetpack-compose","tag-android-image-loading","tag-android-performance","tag-coil","tag-image-caching","tag-jetpack-compose","tag-kotlin","tag-lazycolumn","tag-lazyverticalgrid","tag-memory-optimization"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=242"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/242\/revisions\/244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/243"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}