<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by kazi md. Saidul on Medium]]></title>
        <description><![CDATA[Stories by kazi md. Saidul on Medium]]></description>
        <link>https://medium.com/@kazimdsaidul?source=rss-5fcb081b3848------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FNOpudoAch-oXcq99KB7Sw.jpeg</url>
            <title>Stories by kazi md. Saidul on Medium</title>
            <link>https://medium.com/@kazimdsaidul?source=rss-5fcb081b3848------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:27:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kazimdsaidul/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Real-Life Story: Tackling java.lang.OutOfMemoryError]]></title>
            <link>https://medium.com/@kazimdsaidul/real-life-story-tackling-java-lang-outofmemoryerror-efb809749a62?source=rss-5fcb081b3848------2</link>
            <guid isPermaLink="false">https://medium.com/p/efb809749a62</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[outofmemoryerror]]></category>
            <category><![CDATA[compose]]></category>
            <dc:creator><![CDATA[kazi md. Saidul]]></dc:creator>
            <pubDate>Sun, 12 Jan 2025 10:45:39 GMT</pubDate>
            <atom:updated>2025-01-12T10:45:39.445Z</atom:updated>
            <content:encoded><![CDATA[<h3>Real-Life Story: Tackling java.lang.OutOfMemoryError on Low-End Devices for a Production-Grade Android App</h3><h3>The Challenge: Making an OTT App Accessible to Low-End Devices</h3><p>Recently, our team faced a critical challenge while optimizing our OTT platform app for low-end devices. Users with older Android versions (5.0 and 6.0) and devices with limited RAM (1GB to 2GB) were encountering frequent crashes due to java.lang.OutOfMemoryError.</p><h3>Collecting Reports: Firebase Crash Analysis</h3><p>Analyzing Firebase crash data revealed the following device and OS distribution:</p><ul><li><strong>Galaxy J5, Android 5.1.1</strong>: 38% of affected devices</li><li><strong>Android 6.0</strong>: 31%</li><li><strong>Android 5.0</strong>: 29%</li><li><strong>Android 7.0</strong>: 2%</li></ul><p>We aimed to understand the OutOfMemoryError more deeply using Android Studio’s Memory Profiler to analyze memory usage patterns.</p><h3>The Core Issue Highlighted by Logcat:</h3><pre>Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 111362060 byte allocation with 16777120 free bytes and 60MB until OOM<br>Fatal Exception: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available</pre><p>A java.lang.OutOfMemoryError occurs when the Java Virtual Machine (JVM) cannot allocate enough memory for an object. This often arises due to excessive memory usage, inefficient garbage collection (GC), or heavy image/data processing.</p><h3>Our App’s Context</h3><p>Our application is built with Kotlin, leveraging Jetpack Compose and XML layouts in an MVVM-based clean architecture. The problem was exacerbated by memory-intensive image rendering in Jetpack Compose.</p><h3>Root Cause Analysis: What Went Wrong?</h3><ol><li><strong>Inefficient Image Handling</strong>:</li></ol><ul><li>Large images were loaded without proper caching or compression.</li><li>Jetpack Compose’s Image composables consumed memory directly, bypassing disk or memory caches.</li></ul><ol><li><strong>Garbage Collection Bottleneck</strong>:</li></ol><ul><li>The app struggled to reclaim memory quickly, especially when dealing with multiple images simultaneously.</li></ul><h3>The Solution: Optimizing Image Loading with Coil</h3><p>We addressed the issue by replacing our image-loading strategy with the <strong>Coil</strong> library. Coil is designed for efficient memory management, caching, and smoother image rendering.</p><h3>Implementation</h3><h4>Before: Local Image Loading</h4><pre>@Composable<br>fun CustomImage(<br>    @DrawableRes imageId: Int,<br>    modifier: Modifier = Modifier,<br>    contentScale: ContentScale = ContentScale.Fit,<br>    colorFilter: ColorFilter? = null,<br>    contentDescription: String? = null<br>) {<br>    Image(<br>        painterResource(id = imageId),<br>        modifier = modifier,<br>        contentScale = contentScale,<br>        contentDescription = contentDescription,<br>        colorFilter = colorFilter<br>    )<br>}</pre><h4>After: Optimized Local Image Loading with Coil</h4><pre>@Composable<br>fun CustomImage(<br>    @DrawableRes imageId: Int,<br>    modifier: Modifier = Modifier,<br>    contentScale: ContentScale = ContentScale.Fit,<br>    colorFilter: ColorFilter? = null,<br>    contentDescription: String? = null,<br>    alignment: Alignment = Alignment.Center<br>) {<br>    AsyncImage(<br>        alignment = alignment,<br>        model = ImageRequest.Builder(LocalContext.current)<br>            .data(imageId)<br>            .build(),<br>        contentDescription = contentDescription,<br>        modifier = modifier,<br>        contentScale = contentScale,<br>        colorFilter = colorFilter<br>    )<br>}</pre><h4>Before: Remote Image Loading</h4><pre>@Composable<br>fun CustomImageAsync(<br>    imageUrl: String,<br>    modifier: Modifier = Modifier,<br>    contentScale: ContentScale = ContentScale.Crop,<br>    size: Int = 512,<br>    contentDescription: String? = null,<br>) {<br>    val context = LocalContext.current<br>    val imageRequest = ImageRequest.Builder(context)<br>        .data(imageUrl)<br>        .crossfade(true)<br>        .build()<br><br>    Box {<br>        CustomImage(<br>            imageId = R.drawable.ic_placeholder,<br>            contentDescription = contentDescription,<br>            modifier = modifier,<br>            contentScale = contentScale<br>        )<br><br>        AsyncImage(<br>            model = imageRequest,<br>            modifier = modifier,<br>            contentScale = contentScale,<br>            contentDescription = contentDescription,<br>        )<br>    }<br>}</pre><h4>After: Optimized Remote Image Loading with Coil</h4><pre>@Composable<br>fun CustomImageAsync(<br>    imageUrl: String,<br>    modifier: Modifier = Modifier,<br>    contentScale: ContentScale = ContentScale.Crop,<br>    size: Int = 512,<br>    contentDescription: String? = null,<br>) {<br>    val context = LocalContext.current<br>    val imageRequest = ImageRequest.Builder(context)<br>        .data(imageUrl)<br>        .memoryCacheKey(imageUrl)<br>        .diskCacheKey(imageUrl)<br>        .placeholder(R.drawable.ic_placeholder)<br>        .error(R.drawable.ic_placeholder)<br>        .size(size)<br>        .diskCachePolicy(CachePolicy.ENABLED)<br>        .memoryCachePolicy(CachePolicy.ENABLED)<br>        .build()<br><br>    Box {<br>        Image(<br>            painterResource(id = R.drawable.ic_placeholder),<br>            contentDescription = contentDescription,<br>            modifier = modifier,<br>            contentScale = contentScale<br>        )<br><br>        AsyncImage(<br>            model = imageRequest,<br>            modifier = modifier,<br>            contentScale = contentScale,<br>            contentDescription = contentDescription,<br>        )<br>    }<br>}</pre><h3>Results: A Crash-Free Experience</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XmYCOORkf8e--K665zI7JA.png" /></figure><ul><li><strong>Crash-Free Metrics</strong>: 99.97% crash-free users and 99.98% crash-free sessions.</li><li><strong>Memory Optimization</strong>: Memory consumption reduced by approximately 35%.</li><li><strong>Enhanced User Experience</strong>: Images loaded faster with smooth transitions, and placeholder images improved perceived performance and reduced latency.</li></ul><h3>Key Takeaways</h3><ol><li><strong>Leverage Libraries for Optimized Image Loading</strong>:</li></ol><ul><li>Libraries like Coil or Glide efficiently handle caching and memory management.</li></ul><p><strong>2. Optimize Image Resources</strong>:</p><ul><li>Compress and resize images to align with device capabilities.</li><li>Implement disk and memory caching policies to reduce memory consumption.</li></ul><p><strong>3. Proactive Monitoring and Troubleshooting</strong>:</p><ul><li>Use tools like Firebase Crashlytics to monitor crash patterns and address issues promptly.</li><li>Android Studio’s profiler tools help us understand memory usage.</li></ul><p>By tackling OutOfMemoryError with focused optimization, we ensured that our app became accessible to users on low-end devices without sacrificing performance or user experience.</p><p>#Android #Coil #JetpackCompose #Optimization</p><p>By tackling OutOfMemoryError with focused optimization, we ensured that our app became accessible to users on low-end devices without sacrificing performance or user experience.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efb809749a62" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>