GC does not remove objects because they are “old.” It removes objects that are unreachable. That is the key idea. The JVM starts from GC Roots like: stack variables, static fields, active threads, and JNI references. Then it checks: Is there still a live path from any GC Root to this object? If yes, the object stays alive. If no, it becomes eligible for garbage collection. So “orphan” or “zombie” object usually means: an object that no longer has any reachable reference path from GC Roots. How do you keep an object from removal? Keep it reachable. Common ways: hold a live reference store it in a collection keep it in a static field reference it from another live object But there is an important warning: If you keep references longer than needed, GC cannot free them. That is how memory leaks often happen in Java too. The easiest rule to remember is: Reachable = alive Unreachable = collectible Which JVM topic should I explain next: GC roots, heap vs stack, or weak references? #Java #JVM #GarbageCollection #JavaDeveloper #BackendDevelopment #SoftwareEngineering #MemoryManagement #Programming #TechLearning #ComputerScience
Madhana Gopal Thirunavukkarasu’s Post
More Relevant Posts
-
𝑾𝒉𝒂𝒕 𝒂𝒄𝒕𝒖𝒂𝒍𝒍𝒚 𝒉𝒂𝒑𝒑𝒆𝒏𝒔 𝒘𝒉𝒆𝒏 𝑱𝑽𝑴 𝒓𝒖𝒏𝒔 𝒚𝒐𝒖𝒓 𝑱𝒂𝒗𝒂 𝒑𝒓𝒐𝒈𝒓𝒂𝒎 ? 𝐖𝐞 𝐚𝐥𝐥 𝐰𝐫𝐢𝐭𝐞 "𝐦𝐚𝐢𝐧()" 𝐦𝐞𝐭𝐡𝐨𝐝 𝐁𝐮𝐭 𝐡𝐚𝐯𝐞 𝐲𝐨𝐮 𝐞𝐯𝐞𝐫 𝐭𝐡𝐨𝐮𝐠𝐡𝐭 𝐰𝐡𝐚𝐭 𝐉𝐕𝐌 𝐝𝐨𝐞𝐬 𝐢𝐧𝐭𝐞𝐫𝐧𝐚𝐥𝐥𝐲 𝐭𝐨 𝐬𝐭𝐚𝐫𝐭 𝐲𝐨𝐮𝐫 𝐩𝐫𝐨𝐠𝐫𝐚m ? 🔍 Let’s break it down simply 📝 𝐒𝐭𝐞𝐩-𝐛𝐲-𝐒𝐭𝐞𝐩 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 1️⃣ Class Loading ▫️ JVM loads the class into memory 2️⃣ Method Lookup ▫️ JVM looks for exact method signature: 𝐩𝐮𝐛𝐥𝐢𝐜 𝐬𝐭𝐚𝐭𝐢𝐜 𝐯𝐨𝐢𝐝 𝐦𝐚𝐢𝐧(𝐒𝐭𝐫𝐢𝐧𝐠[] 𝐚𝐫𝐠𝐬) ▫️ Signature must match exactly 3️⃣ No Object Creation ▫️JVM does NOT create an object ▫️Because "main()" is static ▫️It is called using class name directly 4️⃣ Method Invocation ▫️ JVM directly calls 5️⃣ Execution Starts ▫️ Program execution begins from "main()" 🧭 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐈𝐧𝐬𝐢𝐠𝐡𝐭 "main()" is the entry point of Java program ▫️JVM specifically looks for this method ▫️ If not found → program will not run ❌ 🔁 𝐋𝐨𝐚𝐝 𝐂𝐥𝐚𝐬𝐬 → 𝐅𝐢𝐧𝐝 𝐦𝐚𝐢𝐧() → 𝐂𝐚𝐥𝐥 𝐝𝐢𝐫𝐞𝐜𝐭𝐥𝐲 → 𝐄𝐱𝐞𝐜𝐮𝐭𝐞 ♣️ We write just one method but JVM does a lot behind the scenes to run it. 🎈Did you know this flow before? Or just wrote "main()" without thinking much? Let’s discuss 💬 #Java #JVM #JavaDeveloper #Programming #TechJourney #LearnBySharing #JavaConcepts #InterviewPrep #BackendDevelopment
To view or add a comment, sign in
-
Day 74 of #100DaysOfLeetCode 💻✅ Solved #162. Find Peak Element problem in Java. Approach: • Used Binary Search technique to efficiently find the peak element • Set two pointers, left at start and right at end of the array • Calculated mid index using safe mid formula • Compared nums[mid] with nums[mid + 1] to determine direction • If mid element is smaller, moved search space to right half • Otherwise, moved search space to left half including mid • Continued until left and right pointers converged • Final position (left == right) represents the peak index Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) 🚀 ✓ Memory: 44.32 MB (Beats 25.49% submissions) Key Learning: ✓ Strengthened understanding of Binary Search on unsorted arrays ✓ Learned how to apply divide-and-conquer beyond traditional searching ✓ Improved intuition for peak finding using neighbor comparison ✓ Practiced optimizing search space instead of linear scanning Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 69 of #100DaysOfLeetCode 💻✅ Solved #349. Intersection of Two Arrays problem in Java. Approach: • Iterated through each element of the first array • Checked if the element exists in the second array • Used a temporary array to store intersection elements • Ensured no duplicates by checking already added elements • Copied the result into a final array of correct size Performance: ✓ Runtime: 4 ms (Beats 35.60%) ✓ Memory: 44.41 MB (Beats 96.71%) Key Learning: ✓ Practiced array traversal using nested loops ✓ Learned how to handle duplicates manually ✓ Improved understanding of set-like operations without using extra data structures Learning one problem every single day 🚀 #Java #LeetCode #DSA #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
When the LCP Matrix meets Disjoint Set Union (DSU) 🧠 I just came across a problem that looked like a typical Dynamic Programming or String challenge at first glance, but the real "Aha!" moment came from realizing it's actually a Grouping Problem. The Challenge: Given an LCP (Longest Common Prefix) matrix, reconstruct the original string. The Intuition: If LCP[i][j] > 0, it tells us one fundamental thing: the character at index i must be the same as the character at index j. Instead of guessing characters, we can treat these "must-match" positions as connected components. This is where Disjoint Set Union (DSU) shines: Union: For every LCP[i][j] > 0, we union index i and j. Assign: We assign characters ('a'-'z') to each unique root found in the DSU. Validate: Since DSU only handles the "equality" constraint, we use a quick O(n²) DP pass to ensure our constructed string actually reproduces the original LCP matrix. Why this works so well: It simplifies the problem from "what character goes here?" to "which indices belong together?" — keeping time complexity at a very efficient O(n² · α(n)). Check out my full breakdown and Java implementation here: https://lnkd.in/gahG-7hN #Java #DataStructures #Algorithms #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 - 36/60 Problem: Indexes of Subarray Sum 🔍 Learned: Since the array contains only non-negative numbers, used the sliding window (two-pointer) approach. Expand the window by increasing the right pointer and shrink it when the sum exceeds the target. 😅 Struggles: Initially thought about prefix sum + hashmap, but that’s unnecessary here. Realizing that non-negative elements allow sliding window made the solution much simpler. 🧠 Key Learning: Sliding window works perfectly when elements are non-negative because the sum behaves predictably. This helps achieve O(n) time complexity efficiently. 📦 Concepts Used: #Arrays #SlidingWindow #TwoPointers #Subarrays #TimeComplexity #DSA Right approach depends on constraints—understanding that is the real skill. 🚀 #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
🚀 Solved “Detect Cycles in 2D Grid” (LeetCode 1559) Worked on a classic graph problem and implemented an efficient DFS-based solution in Java. 🔍 Key idea: Treat the grid as an undirected graph and detect cycles by: Traversing only same-character neighbors Tracking the parent cell to avoid false cycles Identifying a cycle when visiting an already visited node (not parent) ⚡ Insight: Using a recursion stack (like in directed graphs) gives wrong results here — parent tracking is the correct approach. ⏱ Complexity: Time: O(m × n) Space: O(m × n) 📌 Also added a clean, documented version to my GitHub with test cases. 🔗 LeetCode: https://lnkd.in/d6CKa-BN 🔗 GitHub: https://lnkd.in/gnEfmGAg #Java #DSA #GraphAlgorithms #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Today I explored how different number systems — Decimal, Octal, and Hexadecimal — are converted into Binary and how they are actually stored in memory. Key Takeaways: 🔹 All integer types in Java (byte, short, int, long) are stored in Base 2 (Binary format) 🔹 Positive numbers → MSB (Most Significant Bit) = 0 🔹 Negative numbers → Stored using 2’s Complement (MSB = 1) Examples I practiced: byte a = 24 → Stored as binary byte a = -24 → Converted using 2’s complement int a = 045 → Octal (prefix 0) → Binary int a = 0x45 → Hexadecimal (prefix 0x) → Binary Concept Clarity: ✔ Octal → Each digit = 3 bits ✔ Hexadecimal → Each digit = 4 bits ✔ No prefix → Decimal ✔ 0b → Binary (Java 7+) Bonus Learning: Real numbers (float, double) use IEEE 754 format Characters in Java use Unicode encoding Understanding how data is stored at the binary level really changes the way I see programming! #Java #Programming #Coding #JavaLearning #DeveloperJourney #100DaysOfCode #ComputerScience #LearningInPublic
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟵/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟰𝟰. 𝗙𝗶𝗻𝗱 𝗦𝗺𝗮𝗹𝗹𝗲𝘀𝘁 𝗟𝗲𝘁𝘁𝗲𝗿 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks easy — but the circular wrap-around is the trap most people miss. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Given a sorted circular array of letters, find the smallest letter strictly greater than the target. If none exists, wrap around to the first letter. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵: ✅ Standard binary search for the first letter > target ✅ If letters[mid] > target → go left (end = mid - 1) ✅ Else → go right (start = mid + 1) ✅ After the loop, start points to the answer ✅ Use start % letters.length to handle the circular wrap-around 𝗧𝗵𝗲 𝗰𝗹𝗲𝘃𝗲𝗿 𝗯𝗶𝘁: If target is greater than or equal to all letters, start lands at letters.length — modulo wraps it back to index 0 automatically. No special case needed. 🔄 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(log n) 📦 Space: O(1) One modulo operation handles the entire circular edge case cleanly. This is binary search with a twist — and the wrap-around trick is worth remembering! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gvK_p44b 11 more days. So close! 💪 #LeetCode #Day89of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
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