My Learning


Java Program Internals: What happens when a Java program is run:

2 Processes:

  1. Compiler Process / JDK
  2. Runtime Process / JRE (JVM+Libraries)

Compiler compiles (Part of JDK):

  1. Java code(.java) is compiled and converted to Byte Code(.class)

Runtime process / JRE:

  1. Class loader loads Byte code to Memory (CL — part of JVM)
  2. Bytecode verifier — Verifies the loaded Byte code
  3. Interpreter — Reads the bytecode and executes the bytecode
  4. Program output

Java Memory Management:

3 Types of memory:

  1. Stack Memory
  2. Heap Memory
  3. Metaspace

Instance variables, methods and object references are stored in Stack memory. Follows LIFO. Create ull references.

Heap Memory:


  1. Young Generation
  2. Old Generation

Young Generation — 3 memories:

  1. EDEN — Objects created are first stored here.
  2. Survivor S0
  3. Survivor S1

When EDEN space is full, minor GC will be triggered. It will shift some objects from EDEN to S0. When S0 is full, objects will be moved to S1.

When Young generation is full, the objects will be shifted to Old Generation with different references.

Whenever garbage collection is performed in Old Generation, it is by major GC. Major GC process old Old generation takes more time.

Memory pool:

Part of heap memory.

Stores immutable objects — string class, string pool.

Defined at the runtime by JVM memory managers.

Permanent generation:

All the metadata of classes and methods is stored. Not part of heap memory. generated in run time.

Method area is present in Permanent generation.

Method area stores —

  1. complete class structure
  2. All static variables
  3. Constant variables

Method area is part of permanent generation; not heap memory.

Runtime Constant pool:

This is also part of method area.

Stores static variables and constant variables.

Java Stack memory:

Used to execute the threads. Threads run on stack memory.


  1. Method specific values.
  2. local variables
  3. References to objects (objects stored in heap memory)
  4. Test t1 = new Test(); t1 stored in stack; “new Test()” stored in heap
  5. Stored in LIFO fashion

Method block is part of Stack memory. It stores the method block.

20th Feb

Solid Principles —

Single Responsibility Principle

Real world example: Hospital, where different people perform different functions.

When SRP is followed:

Dr — Prescribe medicines & perform surgeries

Nurses — Nursing services: providing food and medication

Receptionist — to book appointments.

When SRP is not followed, all these jobs done by single person.

In code:

Single class should single logic / duty / algorithm.


Calculate average of two numbers in an integer array, where are two numbers are ith and jth smallest integers in that array.

Here there are 2 logics — Sorting and calculating average. We should have 2 different classes for each logic.

Open Closed Principle

A class should be —

  1. Open for extension and
  2. Closed for modification



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store