<?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 Anirudh Menon on Medium]]></title>
        <description><![CDATA[Stories by Anirudh Menon on Medium]]></description>
        <link>https://medium.com/@anirudhmenon?source=rss-47b553c8f3e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Anirudh Menon on Medium</title>
            <link>https://medium.com/@anirudhmenon?source=rss-47b553c8f3e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:34:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anirudhmenon/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[Abusing Java — 2 + 1 = 4]]></title>
            <link>https://anirudhmenon.medium.com/abusing-java-2-1-4-3d2296c6f04?source=rss-47b553c8f3e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d2296c6f04</guid>
            <category><![CDATA[int]]></category>
            <category><![CDATA[addition]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[integer]]></category>
            <dc:creator><![CDATA[Anirudh Menon]]></dc:creator>
            <pubDate>Fri, 06 Nov 2020 07:10:00 GMT</pubDate>
            <atom:updated>2020-11-06T07:12:34.454Z</atom:updated>
            <content:encoded><![CDATA[<h3>Abusing Java — 2 + 1 = 4</h3><p>We’re going to do something fun today. In this post we will be looking at something that might not necessarily have any practical application, but will help you understand the advantages (or dangers) of reflection in Java.</p><h3>Prerequisite — A brief summary of the Integer class</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zSCIHt52ViwaSH3FL2QrCQ.png" /></figure><p>In Java 5, Sun introduced a micro-optimisation in the Integer class. A new private nested class named <strong>IntegerCache</strong> was included (remember this since we will be exploiting it later).</p><p>To understand the <strong>IntegerCache</strong> nested class it is very important to understand the code snippets below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KV44rZAWNKjDn4YaGqS1QQ.png" /></figure><p>In the first program the value of <strong>int1</strong> and <strong>int2</strong> is <strong>400 </strong>and <strong>int1 == int2 </strong>evaluates to <strong>false</strong>.</p><p>In the second program we change the value’s of <strong>int1</strong> and <strong>int2</strong> to <strong>100</strong> and now <strong>int1 == int2</strong> evaluates to <strong>true</strong>.</p><blockquote>What sorcery is this?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*iCzPKGj57NzJkcS8E7HGkQ.gif" /></figure><p>The == (equality) operator in Java checks the equality for object references and not values. Since int1 and int2 are different objects the equality operator returns false. So this makes complete sense.</p><p>But the question now remains — <em>Why doesn&#39;t this hold true when the values were changed to 100?</em></p><p><em>This is why understanding the IntegerCache class is important.</em> This class essentially caches integer values that lie between the range of -128 and 127. If you’re still confused, let’s look at the IntegerCache class in more detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJoaYschvg_EPoaOeWYMlQ.png" /><figcaption>The interesting parts of the IntegerCache class</figcaption></figure><p><em>I’ve high-lighted the interesting parts of code with an asterisk.</em></p><p>The <strong>cache[] </strong>array<strong> </strong>will have a fixed size of 256. The for loop will iterate through the entire array and add values from -127 to 128 in the <strong>cache[]</strong> array.</p><p>Lets understand how this cache helps us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_YykukWwu40eR7_djqjk4Q.png" /></figure><p>We observe the code in the <strong>valueOf</strong> function in the Integer class. It is clear that this function does not need to create new integer objects whenever we request a value within the range of -127 to 128. It will simply return the existing object from the cache. This means that there is no need to create new objects at runtime whenever our program requests it. This is the optimisation that we’ve had since Java 5.</p><h3>2+1=4 — The code</h3><p><em>Now that we understand the Integer class better, let’s exploit what we’ve learnt.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DEA6GbFO8OQsKJN7FY2XgA.png" /><figcaption>The main program that outputs 2+1 = 4</figcaption></figure><h4>Step 1 — Get IntegerCache class and required variable</h4><pre>Class integerCacheClass = Class.<em>forName</em>(&quot;java.lang.Integer$IntegerCache&quot;);</pre><pre>Field cacheArray = integerCacheClass.getDeclaredField(&quot;cache&quot;);</pre><p>As mentioned earlier, we’re using reflection to achieve this. The above lines of code will first, return an instance of the <strong>IntegerCache </strong>inner class that we spoke about. The second line of code will return the <strong>cache[]</strong> array.</p><blockquote>No magic yet.</blockquote><h4>Step 2 — Make cache[] accessible</h4><pre>cacheArray.setAccessible(true);<br><br>Integer[] array = (Integer[]) cacheArray.get(integerCacheClass);</pre><p>The cache[] array that we obtained in step 1 is private to the Integer class, so we need to mark it as accessible to access it in our code. If this is not done, then our program will crash.</p><p>The second line of code will return <strong>cache[]</strong> that we previously looked at in the Integer class.</p><h4>Step 3 — The Magic!</h4><p>In the original cache[] array, the value 3 resides in index position 131.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*ORm7r8usQ7XYsQbW3XumzA.png" /><figcaption>The original cache array in IntegerCache class</figcaption></figure><p><em>We’re modifying this array using the code below-</em></p><pre>cache[131] = 4;<br><br>Integer i1 = 2;<br>Integer i2 = 1;<br><br><em>// This will output 4<br></em>System.<em>out</em>.println(Integer.<em>valueOf</em>(i1 + i2));</pre><p>In the first line of code we set the value at 131 to be 4, instead of 3. This is how the cache array looks like now-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*Tz_wO65H9D9iDJzgmjCV6g.png" /></figure><p>The next three lines of code, simply declare two integers with values 2 and 1 respectively, and add them. This will output 4.</p><h4>Conclusion</h4><p>Whenever we use value 3 in the Integer class now, it will always return back 4. This is because of the modification in the cache[] array done in Step 3.</p><p>Even if we were to try, <strong>4*3</strong> would evaluate to <strong>16</strong> and <strong>not 12</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d2296c6f04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Network Requests and battery drain]]></title>
            <link>https://anirudhmenon.medium.com/android-network-requests-and-battery-drain-2af7283f48ec?source=rss-47b553c8f3e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2af7283f48ec</guid>
            <category><![CDATA[network]]></category>
            <category><![CDATA[battery]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Anirudh Menon]]></dc:creator>
            <pubDate>Thu, 22 Oct 2020 12:38:36 GMT</pubDate>
            <atom:updated>2020-10-22T13:10:53.967Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>How do we conserve battery while making network calls?</blockquote><p>This is a thought that most mobile developers are perturbed about. We frantically use complex profiling tools, try to merge multiple network calls or compress the request and response all in an attempt to safeguard the battery life. Today we ask a more pertinent question-</p><blockquote>Why do network calls drain battery?</blockquote><p>To answer this question, it’s imperative that we understand what a <strong>Radio Cell </strong>is.</p><h3>The Radio Cell</h3><p>In very simple words, the radio cell is a chip (or antenna) in your mobile device. When the application makes a network request, the data travels in the form of electrical pulses (digital signals) through the mobile circuit board trying to reach the radio cell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*4_npAl9ywx9u9sqUSueAsA.gif" /></figure><p>When these digital signals reach the radio cell it is converted into radio waves of different frequencies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*nXY7hUPCw27QcM9VXYLVow.gif" /></figure><p>The radio waves emitted by the device travel through the atmosphere where they are picked up by Cell towers and are then relayed to its destination.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*EcBfNp7S9Prk1YscJBwckw.gif" /></figure><h3>The Radio Cell State</h3><p>Now that we understand what a Radio Cell is, let’s understand why this affects our battery. The device Radio cell can be in one of three possible states at any given point of time-</p><ol><li>Standby (or Off) State</li><li>Low State</li><li>Full power (or High) State</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XzPt-wvdZwDUJQ4V0f4I4g.png" /></figure><p>The chart above will help you understand how the different states affect network throughput and the battery consumption.</p><p>It should be obvious looking at the chart that in order to prolong battery life, we should ensure that the Radio cell is in Standby state for as long as possible and in Full power state for the least possible time.</p><blockquote>Now this seems do-able, doesn’t it?</blockquote><h3>Radio Cell State Machine</h3><p>This is the final piece of the puzzle. Let’s understand how the Radio cell transitions between states.</p><h4>Standby state</h4><p>For the sake of simplicity, let’s consider that our mobile device has only one application installed in it and the application is launched.</p><p>So far there have not been any network requests made from the device. Currently, the radio cell is in the Standby state. This means that the radio cell is waiting for the application start making network requests.</p><h4>Transition from Standby to Full power state</h4><p>Now, we click a button that makes a network request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*4tkfILtpl66Wn8qjRjTAmQ.gif" /></figure><p>When this happens the radio cell transitions from Standby state to Full power state. This means that your device is ready to send and receive data.</p><p>In this state we achieve the highest API throughput but also drain the most battery. If we make any more requests over the network the Radio cell will continue to remain in Full power mode.</p><p>It is important to note that this switch in state of the Radio cell will take approximately 2 seconds (usually much less).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9ZueZPB8j21BXqF4lGkq-Q.png" /><figcaption>Device makes API call when Radio cell is in Standby state</figcaption></figure><h4>Transition from Full to Low Power</h4><p>Now let’s assume, that the application does not make any more network calls for the next 5–10 seconds. In this case, the radio cell state will transition from Full Power to Low power.</p><p>Although this state will cause the battery to drain, it is significantly reduced as compared to when the Radio cell is in full power.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3baECrqe4LpVdo5VgxhQXA.png" /><figcaption>Device does not make API calls when Radio cell is in Full power</figcaption></figure><h4>Transition from Low power state</h4><p>In the Low power state there are two possible outcomes that can follow-<br>1. The device makes more network requests<br>2. The device makes no more network requests</p><p>In case 1, the radio cell moves from low back to full power with a latency of approximately 1.5 seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Je8vgMd0H6zuPc5iijvVwg.png" /><figcaption>Device makes API when Radio cell is in Low State</figcaption></figure><p>In the second case, if the the device is stuck on the low power for approximately 30 seconds, the radio cell will move back to the standby mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ng_bquFDtM7TxQ9CA7_J9A.png" /><figcaption>The Radio Cell State Machine</figcaption></figure><h4>Conclusion</h4><p>Observing the state machine diagram, it is clear that the radio takes about 35–40 seconds to power down once it is in its optimal state. This means that if we make network calls every 30 seconds for about 2 mins, the radio is in full power for the entire duration of 2 mins. On the other hand, if we manage to make API requests every 50 seconds, then the radio would have some time to power down thereby conserving battery.</p><p>Your biggest gain in battery efficiency does not necessarily depend on how your application communicates with the servers, but rather how the application communicates with the hardware, in this case — The Radio Cell.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2af7283f48ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The survival of ViewModels]]></title>
            <link>https://anirudhmenon.medium.com/the-survival-of-viewmodels-ad6669ea105?source=rss-47b553c8f3e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad6669ea105</guid>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[configuration]]></category>
            <category><![CDATA[mobi̇le]]></category>
            <dc:creator><![CDATA[Anirudh Menon]]></dc:creator>
            <pubDate>Mon, 19 Oct 2020 14:21:27 GMT</pubDate>
            <atom:updated>2020-10-19T14:21:27.364Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Android’s supermodel is the View Model</blockquote><p>The View Model, introduced in 2017 aims to make android development simpler, faster and more intuitive by helping developers write less code; and it does a fantastic job at that!</p><p>It should be well known to most who read this, that the view model survives the entire lifecycle of the the android view component holding it, especially when a configuration change happens. This means when such a catastrophe occurs the data that is held in a view model can survive.</p><p>In general, when the onDestroy() method of the the activity is called the view model is destroyed too. But here’s the catch, onDestroy() is called when a configuration change happens as well. This got me thinking how does a view model survive this in the first place.</p><p>We usually use a ViewModelFactory to create a ViewModel since that is where a lot of the magic happens. So let’s go ahead and look at the code to set this up.</p><p><strong>Step 1 — Create ViewModel class</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DOq8qDryFyAPb82nmzFbjQ.png" /><figcaption>The ViewModel that will be included in our activity</figcaption></figure><p><strong>Step 2 — Create ViewModelFactory class</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lIRAa2TMLZ_KqxCs5yd91A.png" /><figcaption>ViewModelFactory class (You can have this within your activity itself)</figcaption></figure><p><strong>Step 3 — Create ViewModel instance in MainActivity</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-WNFiuOUtLytqlJfXQY71w.png" /><figcaption>onCreate() method of Activity where we create an instance of ViewModel</figcaption></figure><p>Now we deep-dive into the Android OSP. Let’s open up <strong>ComponentActivity.java</strong> in Android Studio you should a piece of code similar to this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hSimMAFol2pygIW54vkqFQ.png" /></figure><p>If we add a breakpoint in these lines whenever a configuration change happens you would notice that the above snippet is called just before the onDestroy() of our activity is supposed to be called. However when a configuration change happens this statement evaluates to false-</p><blockquote><strong>if (!isChangingConfigurations())</strong></blockquote><p>and the clear method is not called for the associated view model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad6669ea105" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>