<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vijay Vimalananda Guru</title>
    <description>The latest articles on Forem by Vijay Vimalananda Guru (@the_name_is_vijay).</description>
    <link>https://forem.com/the_name_is_vijay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F308081%2F70f381fb-5f4f-45a9-a6c2-36f52e288b11.jpeg</url>
      <title>Forem: Vijay Vimalananda Guru</title>
      <link>https://forem.com/the_name_is_vijay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/the_name_is_vijay"/>
    <language>en</language>
    <item>
      <title>Build Your Own AI Assistant App - No Cloud Required!</title>
      <dc:creator>Vijay Vimalananda Guru</dc:creator>
      <pubDate>Sun, 08 Mar 2026 04:52:58 +0000</pubDate>
      <link>https://forem.com/the_name_is_vijay/build-your-own-ai-assistant-app-no-cloud-required-g55</link>
      <guid>https://forem.com/the_name_is_vijay/build-your-own-ai-assistant-app-no-cloud-required-g55</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article will help you to build your own AI Assistant application using edge devices like Raspberry Pi. I initially began this project with a goal of automating all the smart devices in my home, but we will cover that topic in a further article. Before we started, we need to understand what we are going to build and the prerequisites involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You'll Build
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First API service that communicate between your Android/iOS app with the SLM(Small Language Model) that is hosted in Raspberry Pi. Here, we are going to use Python framework &lt;a href="https://fastapi.tiangolo.com" rel="noopener noreferrer"&gt;FastAPI&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Mobile application(both Android/iOS) using &lt;a href="https://kotlinlang.org/compose-multiplatform/?_cl=MTsxOzE7Q3hqUUdDcWlMczd4elA4c1BhMFdOMFdxUTROdnVOZWxDdkRGeFpjQlcwMjR6aWRzcEgyb204anJYMUJJT1pjOTs=" rel="noopener noreferrer"&gt;Compose Multiplatform&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hardware:&lt;/strong&gt; Although Raspberry Pi 4 (4GB/8GB) is enough, it is better to have Raspberry Pi 5 (4BG/8GB/16GB) with 32GB SD card and active cooler.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Software:&lt;/strong&gt; &lt;a href="https://developer.android.com/studio" rel="noopener noreferrer"&gt;Android Studio&lt;/a&gt; (I'm using Panda), Python version 3.11+, &lt;a href="https://play.google.com/store/search?q=tailscale&amp;amp;c=apps" rel="noopener noreferrer"&gt;Tailscale&lt;/a&gt; app(which we will talk later)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model:&lt;/strong&gt; TinyLlama-1.1B-chat-v1.0 (~680MB of size, quantized, consumes 1.1GB –1.9GB RAM, more than enough for a short queries)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network:&lt;/strong&gt; Pi can be connected over Wi-Fi or Ethernet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operating System:&lt;/strong&gt; &lt;a href="https://www.raspberrypi.com/software/" rel="noopener noreferrer"&gt;Raspberry Pi OS&lt;/a&gt; for Raspberry Pi module and Mac/Windows/Linux for mobile app development.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Frontend:&lt;/strong&gt; Compose Multiplatform(single codebase for Android/iOS)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backend:&lt;/strong&gt; FastAPI(handling async request &amp;amp; model inference)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's begin with Frontend
&lt;/h2&gt;

&lt;p&gt;After installing Android Studio, you can download the source code from my &lt;a href="https://github.com/thenameisvijay/SampleAIAssistant.git" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; or write your own implementation - I'm sure may of you can come up with a better version. To keep this article concise, I won't go further detail about the CMP (Compose Multiplatform) code here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tech Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;UI:&lt;/strong&gt; Compose Multiplatform&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Language:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/getting-started.html" rel="noopener noreferrer"&gt;Kotlin&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Architecture:&lt;/strong&gt; Clean Architecture (Data, Domain, Presentation)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Networking Library:&lt;/strong&gt; &lt;a href="https://ktor.io" rel="noopener noreferrer"&gt;Ktor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Local Database:&lt;/strong&gt; &lt;a href="https://sqldelight.github.io/sqldelight/2.2.1/" rel="noopener noreferrer"&gt;SQLDelight&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependency Injection:&lt;/strong&gt; &lt;a href="https://insert-koin.io/docs/4.0/quickstart/android/" rel="noopener noreferrer"&gt;Koin&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency:&lt;/strong&gt; Kotlin Coroutines &amp;amp; Flow&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;composeApp/src/commonMain&lt;/code&gt;: Shared logic, data models, repositories, and UI components.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;composeApp/src/androidMain&lt;/code&gt;: Android-specific implementations (e.g., Database Driver, MainApplication).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;composeApp/src/iosMain&lt;/code&gt;: iOS-specific implementations and entry points.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;data/&lt;/code&gt;: Network services (AIChatApiService) and repository implementations.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;domain/&lt;/code&gt;: Business logic and Use Cases.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;presentation/&lt;/code&gt;: ViewModels and Compose UI screens.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Important Note
&lt;/h2&gt;

&lt;p&gt;The application communicates with the AI model via REST API. You need to configure the &lt;code&gt;BASE_URL&lt;/code&gt; in &lt;code&gt;AIChatApiService.kt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Use this if you have not setup Tailscale
const val BASE_URL = "http://&amp;lt;YOUR_RASPBERRY_PI_IP&amp;gt;:8000"
// If you have setup Tailscale
const val BASE_URL = "http://&amp;lt;IP_PROVIDED_BY_TAILSCALE&amp;gt;:8000"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we setup backend part in Raspberry Pi, we can replace that URL here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Begin with Backend Setup
&lt;/h2&gt;

&lt;p&gt;To streamline this process, we will directly begin with installation of required packages and setting up AI Engine. Of course implementing API service that communicated between app and the AI model.&lt;/p&gt;

&lt;p&gt;After installing Raspberry Pi OS, switch on the Pi module and open terminal/command prompt. &lt;/p&gt;

&lt;p&gt;Let's begin with hosting the model&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Intall VCS and essential tools for compiling
sudo apt install git build-essential cmake -y

# Download cpp project to run Llama on CPU
git clone https://github.com/ggerganov/llama.cpp

# Once done, go to 
cd llama.cpp
cd models

# Download the model from Huggingface or choose your favourite model(recommanded 0.5B-3B Parameter)
curl -L -o tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf

# Prepare your project for building
cmake -B build

# Compiles the project. This will take some time
cmake --build build --config Release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open new terminal window and run this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Start the model
./llama.cpp/build/bin/llama-server -m ./llama.cpp/models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf -c 512 -t 4 --port 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;br&gt;
Now, we will implement an API that communicates with the model which we hosted in step 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Check for OS update
sudo apt update &amp;amp;&amp;amp; sudo apt upgrade -y

# Make a separate folder for API service. I'm naming it as quanta
mkdir quanta &amp;amp;&amp;amp; cd quanta

# To create isolated virtual environment
python3 -m venv .venv

# Activates virtual environment
source .venv/bin/activate

# Install required packages
pip install fastapi uvicorn requests httpx psutil

# Create a Python file naming as aichatservice
touch aichatservice.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the file aichatservice.py and paste the code available in this &lt;a href="https://github.com/thenameisvijay/AIAssistantService-Pi5.git" rel="noopener noreferrer"&gt;repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save the file and run the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn aichatservice:app --host 0.0.0.0 --port 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try this in any API testing tools like Postman or Rest Client&lt;br&gt;
&lt;strong&gt;Request Type&lt;/strong&gt;: POST&lt;br&gt;
&lt;strong&gt;URL&lt;/strong&gt;: &lt;a href="http://raspberry-pi-id:8000/generate" rel="noopener noreferrer"&gt;http://raspberry-pi-id:8000/generate&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Request Body:
{
  "prompt": "can you explain to me AI in short?"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please take note that whenever you login/restart the Raspberry Pi we have to start the web server using the above uvicorn command(picture 1). We also need to start the LLM server using llama.cpp(picture 2)&lt;/p&gt;

&lt;p&gt;Picture 1&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7tinglneo8y1fse892z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7tinglneo8y1fse892z.png" alt=" " width="413" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 2&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9bxiee2wbe0zh4ddu6k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9bxiee2wbe0zh4ddu6k.png" alt=" " width="582" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are now done with the backend part. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional:&lt;/strong&gt; To make the API accessible outside your local network, you can do port forwarding(router level-not secured)or a tunneling  service. I'm going to use Private VPN (Tailscale). This works securely anywhere.&lt;/p&gt;

&lt;p&gt;Open the terminal and run the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Intall Tailscale 
curl -fsSL https://tailscale.com/install.sh | sh
# After Installation, run this
sudo tailscale up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After  running sudo tailscale up, you will be prompted to authenticate using the link provided. Once authentication is successful, you will be redirected to the Tailscale dashboard where you can find the IP Address of the device connected. Copy it and replace it with URL: &lt;a href="http://ip-provided-by-tailscale:8000/generate.%C2%A0" rel="noopener noreferrer"&gt;http://ip-provided-by-tailscale:8000/generate. &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that is it -  you are all set. Now, it's time to run the application in Android Studio.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Output
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Android&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffevmjhpkqk3x782ib147.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffevmjhpkqk3x782ib147.png" alt=" " width="750" height="1334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1xasf80zrx298ghl0aq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1xasf80zrx298ghl0aq.png" alt=" " width="750" height="1334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;iOS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffn7spcw6frogmovwie8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffn7spcw6frogmovwie8u.png" alt=" " width="800" height="1780"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3p8m3463nqubd3mtlia3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3p8m3463nqubd3mtlia3.png" alt=" " width="800" height="1780"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this article only covers the basics of building you own AI assistant, I didn't focus much on the design, adding comment line, architecure and "Token Streaming". In the next article, we will explore how to communicate with smart devices using Docker and Home Assistant.&lt;/p&gt;

&lt;p&gt;Let me know your thoughts in the comments so that it will help me improve further.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>android</category>
      <category>ios</category>
      <category>raspberrypi</category>
    </item>
    <item>
      <title>Understanding Kotlin's Scope Functions</title>
      <dc:creator>Vijay Vimalananda Guru</dc:creator>
      <pubDate>Mon, 15 Apr 2024 08:33:48 +0000</pubDate>
      <link>https://forem.com/the_name_is_vijay/understanding-kotlins-scope-functions-1bn9</link>
      <guid>https://forem.com/the_name_is_vijay/understanding-kotlins-scope-functions-1bn9</guid>
      <description>&lt;h2&gt;
  
  
  What are Scope Functions?
&lt;/h2&gt;

&lt;p&gt;Scope Functions are a part of Kotlin standard library. It executes a block of code within that context of an object. Using these functions on an object with lambda expression, it provides a temporary scope for that object which is either accessible by &lt;em&gt;it&lt;/em&gt;(default) or &lt;em&gt;this&lt;/em&gt;, depending on function type. With this we can access those objects without their names. Thereby it makes the code simple, clean and easily readable. There are 5 different types of scope functions in Kotlin: let, &lt;em&gt;with&lt;/em&gt;, &lt;em&gt;run&lt;/em&gt;, &lt;em&gt;also&lt;/em&gt; &amp;amp; &lt;em&gt;apply&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to choose what?&lt;/strong&gt; &lt;br&gt;
To choose the right function for your requirement, please check the below table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn4dkxy3uxlubp3ulqlmh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn4dkxy3uxlubp3ulqlmh.png" alt=" " width="557" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;let&lt;/em&gt; is primarily used along with a safe call operator (?.) to prevent &lt;em&gt;NullPointerException&lt;/em&gt;. It will execute the block of code only if the object is not null.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main (){
    val name: String? = null
    println(name!!.length)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Exception in thread "main" java.lang.NullPointerException&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using let function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main (){
    val name: String? = null
    name?.let{
        println(name!!.length) //statement will not execute 
                               //as name is null
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;with:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;with&lt;/em&gt; is used for calling functions on context objects without providing the lambda result. It is used to access members and methods of an object without having refer to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using with function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {
    var name: String = ""
    var age: Int = 0
}

fun main() {
    val person = Person()
    with(person) {
        name = "Vijay"
        age = 30
        println("Name: $name, Age: $age")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Name: Vijay, Age: 30&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Inside the with block, you can directly access the properties (name and age) of the person object without explicitly referencing person. This can lead to more concise and readable code, especially when performing multiple operations on the same object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;run:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;run&lt;/em&gt; is a combination of 'let' and 'with' functions. Imagine a null value is assigned to an object and we want to prevent it from NullPointerException. In this case, we can use &lt;em&gt;run&lt;/em&gt; function with that object. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using run function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {
    var name: String = ""
    var age: Int = 0
}

fun main() {
    val person = Person()
    val result = person.run {
        name = "Vijay"
        age = 30
        "Name: $name, Age: $age"
    }

    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Name: Vijay, Age: 30&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The run function also returns the result of the last expression inside its block, which is a string containing the name and age of the person.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;also:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;also&lt;/em&gt; is used where we want to perform additional operations when we have initialized the object member. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using also function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {
    var name: String = ""
    var age: Int = 0
}

fun main() {
    val person = Person()
    val result = person.also {
        it.name = "Vijay"
        it.age = 30
    }.let {
        "Name: ${it.name}, Age: ${it.age}"
    }
    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Name: Vijay, Age: 30&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;let&lt;/em&gt; function is called on the returned object (person). It takes a lambda with the parameter &lt;em&gt;it&lt;/em&gt;, which refers to the object returned by &lt;em&gt;also&lt;/em&gt;. Inside the &lt;em&gt;let&lt;/em&gt; block, a string is created using the properties of the object returned by &lt;em&gt;also&lt;/em&gt;. The result is assigned to the result variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;apply:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;apply&lt;/em&gt; is mostly used for initializing the objects. It is also used to configure specific attributes of that object. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using apply function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {
    var name: String = ""
    var age: Int = 0
}

fun main() {
    val person = Person()
    val result = person.apply {
        name = "John"
        age = 30
    }.let {
        "Name: ${it.name}, Age: ${it.age}"
    }
    println(result)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Name: Vijay, Age: 30&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference between &lt;em&gt;apply _and _also&lt;/em&gt; is, &lt;em&gt;apply&lt;/em&gt; is more focused on modifying and initializing objects and returning them, while &lt;em&gt;also&lt;/em&gt; is more about performing side effects or additional actions without altering the object and then returning it.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Overview of Garbage Collector(GC)</title>
      <dc:creator>Vijay Vimalananda Guru</dc:creator>
      <pubDate>Sat, 10 Sep 2022 19:25:07 +0000</pubDate>
      <link>https://forem.com/the_name_is_vijay/overview-of-garbage-collectorgc-4j4k</link>
      <guid>https://forem.com/the_name_is_vijay/overview-of-garbage-collectorgc-4j4k</guid>
      <description>&lt;p&gt;In this article we will learn about Garbage Collector(GC) and a little bit of Memory Leak.&lt;/p&gt;

&lt;p&gt;Before we jump into the Garbage Collector we will first learn,&lt;/p&gt;

&lt;h2&gt;
  
  
  What is memory leak?
&lt;/h2&gt;

&lt;p&gt;Memory leak is a type of resource leak that is unused and failed to deallocate or release from memory(RAM). &lt;/p&gt;

&lt;h2&gt;
  
  
  When does memory leak occur and what is the outcome?
&lt;/h2&gt;

&lt;p&gt;Memory leak occurs when GC could not release unused objects/resources. As a result it will reduce the amount of available memory and affect the performance of the application. However, it is not always the case where memory is full; it means there is a memory leak. Sometimes applications might be using a high amount of memory to perform heavy tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to prevent memory leak?
&lt;/h2&gt;

&lt;p&gt;Apart from garbage collector, it is the work of the developer to program in such a way that it releases unneeded resources/objects wherever it is required. Another good way to prevent memory leak is by minimizing the use of static members. &lt;/p&gt;

&lt;p&gt;On rare occasions you would have faced &lt;em&gt;java.lang.OutOfMemoryError&lt;/em&gt; during application runtime. This is because JVM could not able allocate memory(heap space) for that application. Java programmers, please note that calling a static method (System.gc()) manually will not guarantee that it will remove all unreachable objects. At the same time don’t forget garbage collector plays a major role in allocating/deallocating objects from heap memory. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Garbage Collector?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;GC is an automatic memory manager that manages the allocation and release of memory for an application.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Generally, whenever we open an application, the operating system will allocate a certain amount of memory for that application. GC will manage that memory throughout the application lifetime. It will run periodically with certain GC steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Garbage Collector Steps:
&lt;/h2&gt;

&lt;p&gt;Initially, when an object is created in the heap, it will be a head node for the corresponding object and it will create a graph-like structure when the corresponding object holds the reference of others. Having that GC will follow three steps to perform garbage collection. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mark:&lt;/strong&gt; GC will walk through the object graph right from the beginning of the node and mark the objects that are reachable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sweep/Delete:&lt;/strong&gt; It will ignore the objects that are marked as reachable and sweep off unreachable objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compacting:&lt;/strong&gt; The empty space left over after removing the unreachable objects will be defragmented by this step. This step will help to isolate memory that is being used and free. &lt;/p&gt;

&lt;h2&gt;
  
  
  Garbage Collector Graph:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8cpe3zko4eetx8j04tv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8cpe3zko4eetx8j04tv.png" alt=" " width="800" height="643"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The garbage collector traverses through the object graph in memory, starting from GC root to corresponding objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generation of Garbage Collector:
&lt;/h2&gt;

&lt;p&gt;Basically, objects in the heap are divided into generations - Young and Old(Tenured)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrupd1z5osts2v9eo8ye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrupd1z5osts2v9eo8ye.png" alt=" " width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Young Generation:&lt;/strong&gt;&lt;br&gt;
Initially, objects are created in the young generation. It is further divided into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eden space&lt;/li&gt;
&lt;li&gt;Survivor space (from space and to space)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Eden space&lt;/em&gt; is where objects are actually created. When Eden space gets full, GC kicks in and performs a minor garbage collection operation. The objects which are marked as reachable are then moved to different &lt;em&gt;Survivor spaces&lt;/em&gt; and later during minor GC operation, reachable objects will be moving to and fro between survivors. Finally, after several generations it will be moved to the old generation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Old Generation:&lt;/strong&gt;&lt;br&gt;
Objects are usually moved from survivor space to the old generation only when the maximum tenuring threshold limit has reached. In the old generation, objects will slowly become unreachable and subjected to major GC operations to free up heap memory allocated for that application. But often that is not the only case where major CG performed. When the heap is full and wants memory to store an object then a major GC operation occurs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Types of GC:
&lt;/h2&gt;

&lt;p&gt;GC in Java are subdivided into:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serial GC:&lt;/strong&gt;&lt;br&gt;
This is the basic GC that runs in a single thread. It is specially designed for smaller applications. All the GC events will happen serially with a single thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parallel GC:&lt;/strong&gt;&lt;br&gt;
This GC is specially designed for large applications that run on multiprocessor or multithreaded environments. Multiple threads are used for minor garbage collection that occurs in young generation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parallel Old GC:&lt;/strong&gt;&lt;br&gt;
It is the same as Parallel GC except that it uses multiple threads for both young and old generations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concurrent GC:&lt;/strong&gt;&lt;br&gt;
In a concurrent collector, multiple threads are used in minor garbage collection with the same algorithm used in parallel GC. It runs concurrently along with application threads to avoid “stop the world” situation. This GC uses more CPU than any other GCs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Garbage First GC:&lt;/strong&gt;&lt;br&gt;
This is specially designed for multithreaded applications that have larger heap size. This GC is quite different from other GCs as there won’t be Young and Old generation. Instead there will be a set of equal size regions and uses multiple threads to scan them. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Epsilon GC:&lt;/strong&gt;&lt;br&gt;
This was released along with JDK 11. It handles memory allocation but doesn’t provide a memory reclamation mechanism. Once the allocated heap is full, JVM will shutdown. This GC is very rarely used and fit only for ultra-latency-sensitive applications.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ZGC:&lt;/strong&gt;&lt;br&gt;
This was released along with JDK 11 and improvised in JDK 12. This is mainly intended for applications which require low latency and/or use a very large heap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shenandoah:&lt;/strong&gt;&lt;br&gt;
This was released along with JDK 12. It is more intensive than any other GCs as it cleans garbage, compact live objects, and free up RAM immediately back to OS. &lt;/p&gt;

&lt;h2&gt;
  
  
  Short Summary:
&lt;/h2&gt;

&lt;p&gt;GC is an automatic memory manager that performs certain operations to allocate and deallocate objects from heap memory. Objects that are reachable will be kept in the memory and objects that are unreachable will be removed to free up space for the application. GC will perform this action until the application lifetime so that it runs smoothly. &lt;/p&gt;

&lt;p&gt;I hope this article helped you to understand something about Garbage Collector.&lt;/p&gt;

&lt;p&gt;Happy learning!!&lt;/p&gt;

</description>
      <category>garbagecollector</category>
      <category>memoryleak</category>
      <category>java</category>
      <category>android</category>
    </item>
    <item>
      <title>All about Android Runtime</title>
      <dc:creator>Vijay Vimalananda Guru</dc:creator>
      <pubDate>Tue, 05 Apr 2022 11:51:05 +0000</pubDate>
      <link>https://forem.com/the_name_is_vijay/all-about-android-runtime-2j45</link>
      <guid>https://forem.com/the_name_is_vijay/all-about-android-runtime-2j45</guid>
      <description>&lt;p&gt;In this post, we are going to learn about Dalvik and Android Runtime. &lt;/p&gt;

&lt;p&gt;Most of the Android developers would have heard about the terms &lt;em&gt;Dalvik, ART, JIT and AOT&lt;/em&gt;. If you ever wonder what these are then this post will help you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Whenever APK is generated, part of that contains .dex files. Those files hold source code of the app as well as libraries that are used. These files are written using low-level code designed for a software interpreter which is called bytecode. When we run the app, the bytecodes which are written in the .dex file are translated by Android Runtime into machine code. This machine code provides instructions that can be directly understood by the machine and are processed by the CPU. &lt;/p&gt;

&lt;p&gt;The compilation of bytecode into machine code can be done in various methods. To understand why Android Runtime came into action we have to move back in time and learn about Dalvik. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Dalvik Virtual Machine(DVM)?
&lt;/h2&gt;

&lt;p&gt;Initially, when Android phones came to the market they were not as efficient as they are now. It is not only just because they have low RAM but also they used a compatible runtime called Dalvik. It is implemented to optimize particularly for one parameter - RAM usage. So instead of compiling the whole app to machine code they used the strategy called &lt;strong&gt;Just-In-Time(JIT)&lt;/strong&gt; compilation. &lt;/p&gt;

&lt;p&gt;With this JIT, the compiler works  quite pretty like an interpreter. It compiles small chunks of code during the execution of the app. This helped Dalvik to save a lot of RAM. But this strategy has a serious drawback. Since this all happened at runtime it had a negative impact on runtime performance. Gradually, they optimized and enhanced Dalvik as a result some of the frequently run code was cached and not recompiled without a need. But still there was a boundary in that case because of limited RAM size.&lt;/p&gt;

&lt;p&gt;In later days, apps were also getting bigger and that impacted the performance and caused problems for Dalvik. So, they introduced a new runtime called &lt;strong&gt;Android Runtime in Android L&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Android Runtime(ART)?
&lt;/h2&gt;

&lt;p&gt;The way ART worked in Android Lollipop was completely different from what we saw in Dalvik. Like JIT in Dalvik, ART(Lollipop and Marshmallow) used a strategy called &lt;strong&gt;Ahead-of-Time(AOT)&lt;/strong&gt; compilation. With this, instead of interpreting code at runtime code was compiled and converted to machine code before running the app. This approach widely improved runtime performance and is more efficient than JIT. Unfortunately, this also comes with some drawbacks as it consumes a lot more memory than Dalvik. Also, it took more time to install the app because the whole app needed to be transformed to the machine code plus it took more time to perform system update because all apps need to be reoptimized. To overcome this downside, JIT compilation was introduced back to ART along with a feature called &lt;strong&gt;Profile-guided&lt;/strong&gt; compilation. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Profile-Guided Compilation?
&lt;/h2&gt;

&lt;p&gt;Generally, most parts of the apps are rarely used. So, having the whole app precompiled was an overhead. That's why in Android Nougat, JIT was brought back to ART and modernized with another strategy called Profile-Guided Compilation.&lt;/p&gt;

&lt;p&gt;Profile-guided compilation technique constantly improved the performance of Android apps as they run. Whenever the app is compiled, it automatically detects some methods that are frequently used. The pre-compile methods are cached and gave best performance. While other parts of the app stay uncompiled until they are actually used. This strategy provided excellent possible performance for key parts of the app while reducing the RAM usage. &lt;/p&gt;

&lt;p&gt;After this change, ART doesn't impact the speed of app installation and system updates. The best part here is, the pre-compilation happens only while the device is charging or idle. But the weak part of this approach is that the user has to actually use the app in order to get the profile data as well as frequently used classes and methods that are pre-compiled. That means that the first few usage of the app can be pretty slow because JIT compilation is used then. To improve this initial user experience, ART was upgraded with a new feature called &lt;strong&gt;Profiles in the cloud&lt;/strong&gt;. And it was introduced in Android Pie.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Profiles in the Cloud?
&lt;/h2&gt;

&lt;p&gt;As we all know most of the users use the app in a same way. So in order to speed up the performance after installation, profile data of the user who has already used the app are being collected. This collected data is clustered into a file called a common core profile. When a new user installs the app, this file is downloaded along with the application. With the help of this file, ART can easily detect the pre-compiled classes and methods that are mostly used by the user. This approach helped new users to get startup and steady state performance when they install the app. Later, user-specific profile data is being collected and recompilation of the code is done for this particular user when the device is idle or charging. As all these things are done behind the scene by ART we don’t need to do anything manually to enable it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Short Summary:
&lt;/h2&gt;

&lt;p&gt;Dalvik VM was using JIT till KitKat. As it has some downsides Android Runtime(ART) was introduced in Lollipop to overcome the issue that Dalvik had. At the beginning ART used a technique called Ahead-of-Time(AOT). It also had some drawbacks like high RAM usage and more installation time so ART was enhanced with a strategy called Profile-Guided Compilation. Later it was upgraded and came up with Profiler in the Cloud.&lt;/p&gt;

&lt;p&gt;Happy learning!!&lt;/p&gt;

</description>
      <category>android</category>
      <category>androidruntime</category>
      <category>dalvik</category>
    </item>
  </channel>
</rss>
