<?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 Manoj Kumar on Medium]]></title>
        <description><![CDATA[Stories by Manoj Kumar on Medium]]></description>
        <link>https://medium.com/@jonamramuk222?source=rss-78292c30db6c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Eh8qnpJ17vmks6Ol</url>
            <title>Stories by Manoj Kumar on Medium</title>
            <link>https://medium.com/@jonamramuk222?source=rss-78292c30db6c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:00:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jonamramuk222/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[Removing Duplicates from an Array in Kotlin: A Comprehensive Guide]]></title>
            <link>https://medium.com/@jonamramuk222/removing-duplicates-from-an-array-in-kotlin-a-comprehensive-guide-f390eef3ee52?source=rss-78292c30db6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f390eef3ee52</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Manoj Kumar]]></dc:creator>
            <pubDate>Tue, 08 Oct 2024 11:48:02 GMT</pubDate>
            <atom:updated>2024-10-08T11:48:02.060Z</atom:updated>
            <content:encoded><![CDATA[<p>Kotlin is a modern programming language that offers powerful tools and functions to manipulate data structures effectively. One common task is removing duplicates from an array. In this blog post, we will explore four different methods to achieve this: distinct(), toSet(), toMutableSet(), and toHashSet(). We will discuss each method, provide examples, and suggest scenarios for when to use each one.</p><h3>1. Using distinct()</h3><p>The distinct() function returns a list containing only unique elements from the original collection while maintaining the order of the first occurrence.</p><p><strong>Example </strong>:</p><blockquote>fun main() {<br> val names = arrayOf(“Manoj”, “Nithin”, “Nithin”, “Anand”, “Monil”, “Monil”, “Zaid”)<br> val uniqueNames = names.distinct()<br> println(uniqueNames) // Output: [Manoj, Nithin, Anand, Monil, Zaid]<br>}</blockquote><h3>When to Use</h3><ul><li>Use distinct() when you need to maintain the order of elements as they appear in the original array.</li><li>It is a good choice for lists where the order is significant.</li></ul><h3><strong>2. Using </strong><strong>toSet()</strong></h3><p>The toSet() function converts the array into a Set, which inherently does not allow duplicate elements. The order of elements is not guaranteed since Set does not maintain order.</p><p><strong>Example :</strong></p><blockquote>fun main() {<br> val names = arrayOf(“Manoj”, “Nithin”, “Nithin”, “Anand”, “Monil”, “Monil”, “Zaid”)<br> val uniqueNames = names.toSet()<br> println(uniqueNames) // Output: [Manoj, Nithin, Anand, Monil, Zaid] (order may vary)<br>}</blockquote><h3>When to Use</h3><ul><li>Use toSet() when you don&#39;t care about the order of elements and just want to eliminate duplicates.</li><li>This is efficient for larger datasets where order is not a concern.</li></ul><h3>3. Using toMutableSet()</h3><p>Similar to toSet(), toMutableSet() converts the array into a mutable set, allowing you to modify the set after its creation.</p><p><strong>Example :</strong></p><blockquote>fun main() {<br> val names = arrayOf(“Manoj”, “Nithin”, “Nithin”, “Anand”, “Monil”, “Monil”, “Zaid”)<br> val uniqueNames = names.toMutableSet()<br> uniqueNames.add(“Deepak”) // Adding new element<br> println(uniqueNames) // Output: [Manoj, Nithin, Anand, Monil, Zaid, Deepak] (order may vary)<br>}</blockquote><h3>When to Use</h3><ul><li>Use toMutableSet() when you want the flexibility to modify the collection after removing duplicates.</li><li>Ideal for scenarios where you may need to add or remove elements later on.</li></ul><h3><strong>4. Using </strong><strong>toHashSet()</strong></h3><p>The toHashSet() function creates a HashSet, which is an unordered collection of unique elements. Like toSet(), it does not maintain any order.</p><p><strong>Example :</strong></p><blockquote>fun main() {<br> val names = arrayOf(“Manoj”, “Nithin”, “Nithin”, “Anand”, “Monil”, “Monil”, “Zaid”)<br> val uniqueNames = names.toHashSet()<br> println(uniqueNames) // Output: [Manoj, Nithin, Anand, Monil, Zaid] (order may vary)<br>}</blockquote><h3>When to Use</h3><ul><li>Use toHashSet() when you want to quickly eliminate duplicates without any concern for element order.</li><li>This is often the fastest option for removing duplicates due to the underlying hash table structure.</li></ul><h3>Conclusion</h3><p>Removing duplicates from an array in Kotlin can be done efficiently using various methods depending on your specific requirements. Here’s a quick recap:</p><ul><li>Use <strong>distinct()</strong> to maintain order.</li><li>Use <strong>toSet()</strong> when order is not a concern.</li><li>Use <strong>toMutableSet()</strong> for a mutable collection after removing duplicates.</li><li>Use <strong>toHashSet()</strong> for quick elimination of duplicates with no order requirement.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f390eef3ee52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Garbage Collection and Frame Rendering in Android: How to Avoid UI Lag]]></title>
            <link>https://medium.com/@jonamramuk222/understanding-garbage-collection-and-frame-rendering-in-android-how-to-avoid-ui-lag-b73ab52fe66e?source=rss-78292c30db6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b73ab52fe66e</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[memory-improvement]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Manoj Kumar]]></dc:creator>
            <pubDate>Sat, 05 Oct 2024 18:19:03 GMT</pubDate>
            <atom:updated>2024-10-05T18:26:23.478Z</atom:updated>
            <content:encoded><![CDATA[<p>For Android developers, creating smooth and responsive user interfaces (UI) is one of the most important goals. However, achieving a seamless experience often comes with challenges like frame drops, UI stuttering, and lag. One common culprit behind these performance issues is <strong>Garbage Collection (GC)</strong>. In this blog post, we’ll explore what GC is, how it impacts your app’s performance, and tips to prevent it from causing UI lag.</p><h4>What is Garbage Collection in Android?</h4><p>The <strong>Garbage Collector (GC)</strong> in Android is responsible for <strong>automatic memory management</strong>. It tracks and frees memory used by objects that are no longer needed in the app. This prevents memory leaks and optimizes the overall memory usage. However, if not handled carefully, the GC can cause performance issues, especially in scenarios requiring smooth and continuous operations, such as UI rendering.</p><h4>The 16ms Rule for Smooth UI</h4><p>Before diving into the GC’s impact on performance, it’s important to understand why timing is so critical in Android apps. Modern Android devices usually operate with a <strong>60Hz display refresh rate</strong>, meaning they refresh the screen 60 times per second.</p><p>To ensure <strong>smooth rendering</strong>, your app must update the UI every <strong>16.67 milliseconds</strong> (approximately 16ms) to maintain a frame rate of <strong>60 FPS (Frames Per Second)</strong>. Missing this deadline leads to <strong>dropped frames</strong> or <strong>jank</strong>, where animations or interactions feel laggy.</p><p>Now, how does the garbage collector fit into this?</p><h4>How Does Garbage Collection Cause App Lag?</h4><p>Even though GC is essential for managing memory, it can introduce <strong>lag</strong> when the garbage collection process pauses the app’s execution, especially if it runs on the <strong>main (UI) thread</strong>. Let’s explore how this happens:</p><ol><li><strong>GC Pauses the Main Thread</strong>: Garbage collection can temporarily halt your app’s main thread, the thread responsible for updating the UI. When the GC runs, it may take anywhere from a few milliseconds to longer, depending on the memory usage and the number of objects it needs to process. If GC interrupts the UI thread for too long, it can cause your app to <strong>miss its 16ms rendering window</strong>, leading to stuttering or dropped frames.</li><li><strong>Frequent GC Calls</strong>: If your app creates and discards many objects frequently (also known as <strong>high memory churn</strong>), the GC will be triggered more often. This can result in frequent GC pauses, significantly degrading the user experience. For instance, repeatedly creating objects within a <strong>RecyclerView</strong> or during animations without recycling them can cause this problem.</li><li><strong>Large Heap Sizes</strong>: When your app uses large or inefficient data structures, the GC process takes longer. The larger the memory heap, the more time the garbage collector needs to scan and clean up unreferenced objects. On devices with limited memory, this can lead to more noticeable slowdowns.</li></ol><h4>How to Minimize GC-Induced Lag</h4><p>Now that we understand the role of GC in UI lag, let’s look at some strategies to minimize its impact and ensure smooth frame rendering.</p><p>1. <strong>Minimize Object Creation</strong></p><p>One of the key practices to avoid frequent GC triggers is to <strong>minimize object creation</strong>, particularly within performance-critical sections like animations, scrolling, or touch events. Instead of constantly creating new objects, try reusing existing ones or using <strong>object pools</strong>.</p><p>For instance:</p><ul><li><strong>RecyclerView</strong> efficiently reuses views to minimize object creation.</li><li>Caching strategies for images or data that are repeatedly used can also reduce memory allocations.</li></ul><p>2. <strong>Avoid Memory Leaks</strong></p><p>Memory leaks are when your app holds on to unnecessary objects for too long, preventing the GC from reclaiming memory. Tools like <strong>LeakCanary</strong> can help identify and fix memory leaks before they become problematic.</p><p>A common cause of memory leaks is <strong>improper reference handling</strong> (like holding references to activity or fragment contexts in long-lived objects). By resolving these leaks, you can reduce the pressure on the garbage collector, making your app more efficient.</p><p>3. <strong>Use Background Threads</strong></p><p>Offload expensive operations (like network calls, image processing, or database queries) to <strong>background threads</strong>. This keeps the UI thread free to handle rendering and user interactions, reducing the chances that GC will run during critical UI operations.</p><p>You can use frameworks like <strong>Kotlin Coroutines</strong> or <strong>RxJava</strong> to manage background operations effectively.</p><p>4. <strong>Use the Profiler to Monitor GC Activity</strong></p><p>Android Studio provides powerful tools like the <strong>Memory Profiler</strong> to help you monitor your app’s memory usage and GC behavior. You can track how often the GC is running, how much memory it’s reclaiming, and how long the pauses are.</p><p>Additionally, enabling <strong>GC logging</strong> in <strong>Logcat</strong> can give you real-time information on when the garbage collector is running and how long it takes. A typical log entry looks like this:</p><p>D/Gc: GC freed 1024K, 15% free 65536K/81920K, paused 10ms, total 15ms</p><p>Use these tools to identify memory hotspots and optimize your app accordingly.</p><p>5. <strong>Avoid Long-running GC Pauses</strong></p><p><strong>Major GC</strong> (full garbage collection) scans the entire heap, taking more time than <strong>Minor GC</strong> (which only scans recently allocated objects). To avoid long-running pauses from full GC, focus on:</p><ul><li><strong>Keeping heap sizes small</strong> by optimizing memory usage.</li><li><strong>Efficient object lifecycles</strong> so that objects not needed are quickly dereferenced.</li></ul><h4>Conclusion</h4><p>Garbage Collection is a double-edged sword in Android development. While it helps manage memory automatically, it can also disrupt smooth UI rendering if triggered too frequently or during critical operations. By understanding the role of GC and how to manage memory effectively, you can ensure your app meets that critical <strong>16ms per frame</strong> rendering target and provides a smooth, lag-free user experience.</p><p>For Android developers, following best practices like <strong>minimizing object churn</strong>, <strong>avoiding memory leaks</strong>, and <strong>monitoring GC behavior</strong> will go a long way in optimizing performance and keeping your UI running at a smooth 60 FPS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b73ab52fe66e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>