Saquib Aftab

5.8K posts

Saquib Aftab banner
Saquib Aftab

Saquib Aftab

@iamsaquibdev

Lead Software Engineer | ❤️ Programmer I demystify Java, Spring Boot, and System Design.

localhost:8080 Beigetreten Ekim 2023
462 Folgt4.3K Follower
Saquib Aftab
Saquib Aftab@iamsaquibdev·
What are weak, soft, and phantom references in Java?
Saquib Aftab tweet media
English
0
7
42
1.1K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Factory vs Abstract Factory Design Pattern ✅️
Saquib Aftab tweet media
English
0
12
52
2.6K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
What is a ClassLoader? A ClassLoader is a component of the Java Virtual Machine responsible for loading .class files into memory when they are required. When your program references a class, the JVM: 1. Checks if the class is already loaded. 2. If not, it asks a ClassLoader to load it. 3. The class is then verified, linked, and initialized Bootstrap ClassLoader ↑ Platform ClassLoader ↑ Application ClassLoader ↑ Your Custom ClassLoader
English
1
12
82
3.2K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Important Java Interview Question: What’s the difference between young generation and old generation GC in Java? ∙The JVM heap is split into generations based on the observation that most objects die young. ∙Young Generation is where all new objects are allocated. It’s further divided into Eden and two Survivor spaces (S0 and S1). ∙When Eden fills up, a Minor GC is triggered. It’s fast because it only scans the Young Generation, and most objects there are already dead by that point. ∙Objects that survive a Minor GC get moved to a Survivor space. They keep getting copied between S0 and S1 on each Minor GC cycle. ∙Each time an object survives a GC, its age counter increments. Once it crosses the tenuring threshold (default is 15, but the JVM can adjust it dynamically), it gets promoted to the Old Generation. ∙Old Generation holds long-lived objects like caches, static data, or objects that just kept surviving Minor GCs. ∙When the Old Generation fills up, a Major GC (or Full GC) is triggered. This is significantly more expensive because it has to scan a much larger memory region. ∙In G1GC (default since Java 9), the strict physical separation between Young and Old is replaced with a region-based model. Regions are logically assigned as Eden, Survivor, or Old, but any region can be reassigned. The generational concept still holds, just implemented differently. ∙Java 21 introduced Generational ZGC (and Generational Shenandoah is also in progress). These are low-pause collectors that now apply generational collection to ZGC’s region model, giving you both the throughput benefits of generational collection and ZGC’s sub-millisecond pause goals. ∙Minor GC is stop-the-world but very short. Major/Full GC is also stop-the-world (for most collectors) and noticeably longer. ∙The GC algorithm used for each generation can differ. For example, with G1, Young collections use an evacuating collector, while Old regions are handled through concurrent marking and mixed collections. ∙You can tune Young Gen size with -Xmn, or use -XX:NewRatio to set the ratio between Young and Old. But with G1 and ZGC, it’s usually better to let the JVM manage sizing dynamically.
English
0
4
36
1.4K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Java Interview Question ✅ In a Java microservice, you occasionally see ClassNotFoundException or mysterious linkage errors only in production. Locally, everything works, but in containers, some classes are missing at runtime. The classpath looks long and messy, and multiple frameworks are involved. How do you debug and stabilise class loading?
English
0
0
10
580
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Your Java service runs smoothly after a fresh deploy, but memory usage slowly climbs over days until the process is restarted. Heap dumps show many long-lived objects that you expected to be short-lived. You suspect some caches or collections are growing unbounded. How do you locate and fix this memory leak?
English
1
1
23
1.2K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
JIT Compilers: Key Optimisation Techniques 👇
Saquib Aftab tweet media
English
0
5
26
568
Saquib Aftab
Saquib Aftab@iamsaquibdev·
JIT Compiler ✅ When you run a Java program, it doesn't execute your .java code directly. It runs bytecode, a platform-neutral intermediate format. The JVM initially interprets this bytecode line by line, which is slow. The JIT compiler solves this by watching your program run and compiling frequently called methods into native machine code.
English
1
5
44
1.3K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Heap Memory 👇 Stores all objects created with new Shared across threads: Requires synchronisation care Larger, but slower due to GC overhead Managed by the Garbage Collector: Objects live until no references remain Overflow causes OutOfMemoryError
English
0
3
32
1.1K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Stack Memory Vs Heap Memory in Java ✅
Saquib Aftab tweet media
English
0
6
46
764
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Stack Memory 👇 in Java Stores method call frames, local variables, and references Follows LIFO (Last In, First Out): Each method call pushes a frame, and return pops it Fast allocation/deallocation (just a pointer move) Thread-private: Each thread has its own stack Fixed/limited size: Overflow causes StackOverflowError Short-lived: data dies when the method returns
English
1
7
45
1.7K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Interview Question: You use a fixed-size thread pool in your Java app, but during peak traffic requests start timing out. CPU is not fully used, yet threads are always busy. After digging, you realise many tasks are waiting on blocking I/O calls. How do you redesign thread usage to avoid starvation?
English
2
4
46
4.4K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Your Java service runs fine at low load, but under heavy traffic, you see random latency spikes. Heap usage looks normal, but GC logs show frequent stop-the-world pauses. You can’t just throw more memory at it due to cost constraints. How do you tune garbage collection to reduce these pauses?
English
0
6
30
2.1K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
How to debug a java.lang.OutOfMemoryError in Java?
Saquib Aftab tweet media
English
2
8
84
2.6K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
What causes a java.lang.OutOfMemoryError in Java?
Saquib Aftab tweet media
English
1
9
65
11.1K
Saquib Aftab retweetet
Saquib Aftab
Saquib Aftab@iamsaquibdev·
With Spring Boot 4, when you add a starter dependency now, you get exactly what that starter needs and nothing more. Faster startup times: Less classpath scanning means your application boots quicker Reduced memory footprint: Fewer configuration classes loaded into memory Cleaner dependency trees: Easier to debug and understand what’s actually in your application Better native image compilation: GraalVM doesn’t need to process unnecessary metadata
English
3
6
64
3.9K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Java Interview Question: Why Is HashMap Not Thread Safe?
Saquib Aftab tweet media
English
0
8
72
1.6K
Saquib Aftab
Saquib Aftab@iamsaquibdev·
Reactive Libraries in the Java Ecosystem 👇 1. Project Reactor is the foundation for Spring WebFlux and is highly popular. 2. RxJava is another widely used veteran library 3. Spring WebFlux is Spring’s reactive web framework built on top of Reactor.
English
1
1
22
722