Java☕ — Garbage Collection♻️ Earlier, I thought GC was magic. Objects disappear… somehow. Then I learned the basics of how JVM actually cleans memory. Key idea that clicked for me 👇 GC removes objects that are no longer reachable. 📝JVM divides heap into: ✅Young Generation (Eden, Survivor) ✅Old Generation New objects → Young Gen Long-living objects → Old Gen #Java_Code User u = new User(); u = null; // eligible for GC 📝Important realization: You don’t control GC — you design code to help it. 📝Good GC behavior comes from: ✅Fewer unnecessary objects ✅Short object lifetimes ✅Clearing references when done Java isn’t leaking memory. Bad references are. #Java #GarbageCollection #JVM #MemoryManagement
Java Garbage Collection Basics: Understanding Reachability and Heap Management
More Relevant Posts
-
💻 Memory Leak in Java – A Silent Performance Killer Memory leaks in Java don’t happen because GC fails — they happen because objects are still strongly referenced and never become eligible for garbage collection. 🔎 What happens? Heap memory keeps increasing → Full GC runs → Memory doesn’t drop → Eventually leads to OutOfMemoryError. ⚠️ Common Causes: ✔ Static collections holding objects ✔ Unclosed DB connections / streams ✔ ThreadLocal not removed ✔ Unbounded caches ✔ Listeners not deregistered 🛠 How to Identify: ✅ Monitor heap using JConsole / VisualVM ✅ Take heap dump using jmap ✅ Analyze in Eclipse MAT → Check Dominator Tree & GC Roots 💡 Remember: Garbage Collector removes unused objects — But it cannot remove objects you are still referencing. Performance is not just about writing code. It’s about writing memory-efficient code. #Java #MemoryLeak #GarbageCollection #JavaDeveloper #BackendDevelopment #InterviewPreparation 🚀
To view or add a comment, sign in
-
-
Solved a few interesting Array problems in Java 1.Minimum Removals to Balance Array Sorted the array first and applied a sliding window / two-pointer approach Goal was to keep elements where max ≤ min × k and remove the rest 2.Shuffle the Array Rearranged elements from the form [x1,x2…xn,y1,y2…yn] → [x1,y1,x2,y2…] Practiced index manipulation and clean iteration logic Good for understanding array positioning problems 3.Transformed Array (Circular Shift Logic) Implemented shifting based on element values (positive → forward, negative → backward) Used modulo arithmetic for circular indexing Helped in index calculations Trying to stay consistent and learn something new regularly 😊 Code is Available on my GitHub https://lnkd.in/guwCQJcw If anyone has suggestions or knows a better approach, please feel free to share 🙌 #DSA #Java #ArrayProblems #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
✨DAY-21: Threads in Java Be Like… 😅☕ This meme perfectly captures the real struggle of working with Multithreading in Java. At first, threads feel powerful — 💪 “I can do multiple things at once!” Yes! Concurrency improves performance and makes applications faster. But then reality hits… 🚗 ⚠️ Race Conditions – Multiple threads competing for the same resource. 💥 Data Race – When shared data gets corrupted because of improper synchronization. 💀 Waiting for Lock – One thread stuck, endlessly waiting for access. 🔥 And sometimes everything is breaking… but we say: “This is fine…” Finally comes the biggest decision: 🔴 Synchronized – Safe but may reduce performance. 🔴 Unsynchronized – Fast but risky. 👉 This image reminds us that multithreading is powerful, but without proper synchronization (like synchronized, Lock, volatile, etc.), things can go out of control quickly. Lesson: Concurrency is not just about speed — it’s about correctness. #Java #Multithreading #Concurrency #BackendDevelopment #SoftwareEngineering #CodingHumor
To view or add a comment, sign in
-
-
🚀 Container With Most Water | Java | Two Pointer Approach I solved the “Container With Most Water” problem using an optimized Two Pointer technique in Java. The goal is to find two lines that together with the x-axis form a container, such that the container holds the maximum amount of water. 🧠 Approach: Start with two pointers at both ends of the array. Calculate the width between them. The height is determined by the smaller of the two values. Update the maximum area. Move the pointer pointing to the smaller height inward. This greedy strategy works because the area depends on both width and minimum height, and moving the smaller height gives a chance to find a larger area. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Practicing DSA problems daily to improve logical thinking and optimization skills. #Java #DSA #TwoPointer #ProblemSolving #CodingJourney #LeetCode #DataStructures
To view or add a comment, sign in
-
-
💥☕ Troubleshooting OutOfMemoryError in Java Few things are more stressful in production than seeing: java.lang.OutOfMemoryError 😱 But OOM is not just “a memory issue” - it’s usually a design, configuration, or resource management problem hiding underneath 🔍 In my latest blog, I break down the most common types of OOM errors and their real-world causes 👇 📘 What You Will Learn 🧠 1️⃣ Java heap space Common causes of heap memory leaks: 📦 Static fields holding references ⚖️ Incorrect equals() / hashCode() implementations 🧩 Non-static inner classes 🗑️ Misuse of finalize() 🔌 Unclosed streams & database connections 🧵 Improper use of ThreadLocal in thread pools ♻️ 2️⃣ GC Overhead Limit Exceeded When the JVM spends too much time doing GC and recovers too little memory 🧱 3️⃣ Metaspace Class metadata leaks and excessive class loading 📏 4️⃣ Requested Array Size Exceeds VM Limit When allocation size itself becomes the issue 🚀 5️⃣ Direct Buffer Memory Off-heap memory issues (often seen in NIO / Netty applications) Understanding these different OOM scenarios helps you move from panic mode to structured diagnosis 💡 🔗 https://lnkd.in/eGWh9K2X Happy debugging - and may your heap stay healthy and your GC stay calm 😄🔥 #Java #JavaDeveloper #JVM #OutOfMemoryError #MemoryLeak #PerformanceTuning #GarbageCollection #Troubleshooting #BackendDevelopment #SoftwareEngineering #TechBlog #LearnJava #DevCommunity
To view or add a comment, sign in
-
-
Merging Two Arrays in Java :- Problem Overview : Given two arrays, the goal was to merge them into a new array while maintaining the order of elements. This approach is commonly used in algorithms like Merge Sort and helps improve understanding of efficient array traversal. Approach: Created two arrays and a third array to store the merged result. Used three pointers: i → to traverse the first array j → to traverse the second array k → to store elements in the merged array Compared elements from both arrays and inserted the smaller one into the new array. Added remaining elements after one array was fully traversed. Printed the final merged array. #DSA #JAVA
To view or add a comment, sign in
-
-
🚀 Java Deep Dive — Daemon Threads (Something many developers overlook) In Java, not all threads behave the same way. There are two types: • User Threads • Daemon Threads The JVM keeps running as long as at least one user thread is alive. But daemon threads work differently. They are background service threads used for supporting tasks like: • Garbage Collection • Monitoring • Background cleanup If all user threads finish, the JVM will terminate immediately, even if daemon threads are still running. Example: Java Example Thread thread = new Thread(() -> { while(true){ System.out.println("Running..."); } }); thread.setDaemon(true); thread.start(); If the main thread finishes, this daemon thread will not keep the JVM alive. Important rule: You must call "setDaemon(true)" before starting the thread, otherwise Java throws "IllegalThreadStateException". 💡 Key Insight Daemon threads are useful for background tasks that should not block application shutdown. #Java #Multithreading #BackendDevelopment #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
I’ve written a simple blog breaking down the real difference between blocking, parking, and virtual threads in Java — and explaining what exactly gets paused (OS thread vs continuation) when your code “waits.” https://lnkd.in/g5DxUNMW
To view or add a comment, sign in
-
Day 3/50 | #50DaysOfCode 📍 Platform: LeetCode 💻 Language: Java ✅ 2520. Count the Digits That Divide a Number (Easy) Today’s problem focused on digit extraction and divisibility checks. It helped strengthen my understanding of number manipulation and conditional logic. 🔎 Approach: Extract each digit using modulus (%) Check if the digit divides the original number using modulo If divisible, increment the counter Continue until all digits are processed Return the final count 📌 Example: Input: num = 1248 Output: 4 Explanation: All digits (1, 2, 4, 8) divide 1248 evenly This problem improved my understanding of loops, digit handling, and divisibility logic in Java. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #Consistency #LearningJourney #50DaysOfCode #LinkedIn
To view or add a comment, sign in
-
-
Day 10- What I Learned In a Day(JAVA) In the real world, input is not inbuilt-the user has to provide it. Today, I learned how to take user input in Java and understood how the Scanner class works. I learned: ✔ Why Java does not automatically take input ✔ How System.in reads from the keyboard ✔ How nextLine() reads full user input ✔ How to create a Scanner object import java.util.Scanner; class ClassName { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // input here sc.close(); } } and also using the new tool(VSCODE) practiced 👇 #Java #LearningJourney #CodingDaily #JavaDeveloper
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development