Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:45:20+00:00 2026-06-02T22:45:20+00:00

I build a sample program demonstrate memory leak in java. public class MemoryLeakTest {

  • 0

I build a sample program demonstrate memory leak in java.

public class MemoryLeakTest {
     static int depth = 0;
     int number=0;
     MemoryLeakTest mobj;

     MemoryLeakTest(){
      number = depth;
      if(depth < 6500){
          depth++;
          mobj = new MemoryLeakTest();
      }
     }

     protected void finalize(){
      System.out.println(number + " released.");
     }

    public static void main(String[] args) {
        try{
             System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
             System.out.println("Free Memory in starting "+ Runtime.getRuntime().freeMemory());
             MemoryLeakTest testObj = new MemoryLeakTest();
             System.out.println("Free Memory in end "+ Runtime.getRuntime().freeMemory());
             System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
        }
        catch(Exception exp){}
        finally{
            System.out.println("Free Memory"+ Runtime.getRuntime().freeMemory());
            System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
        }
    }

}

I run it by changing value of N in if(depth < N). An here is the result;

when depth is 1000

init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory in starting 15964120
Free Memory in end 15964120
init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory 15964120
init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)

when depth is 1500

init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory in starting 15964120
Free Memory in end 15964120
init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory 15873528
init = 16777216(16384K) used = 379400(370K) committed = 16252928(15872K) max = 259522560(253440K)

when depth is 6000

init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory in starting 15964120
Free Memory in end 15692784
init = 16777216(16384K) used = 560144(547K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory 15692784
init = 16777216(16384K) used = 560144(547K) committed = 16252928(15872K) max = 259522560(253440K)

when depth is 6500 (Exception in thread “main” java.lang.StackOverflowError)

init = 16777216(16384K) used = 288808(282K) committed = 16252928(15872K) max = 259522560(253440K)
Free Memory in starting 15964120
Free Memory in end 15676656
init = 16777216(16384K) used = 576272(562K) committed = 16252928(15872K) max = 259522560(253440K)

My questions are;

  1. It is not calling finalize(). Is it memory leak?
  2. There is not change in free memory up to N=1000. But when N=1500 there is 2 different
    values for used memory at the end of the program ie 282K and 370K.
    Why does it so?
  3. When N=6500, JVM generates error. So why last 2
    statements of try{} are executed.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:45:21+00:00Added an answer on June 2, 2026 at 10:45 pm

    Your program won’t “leak” as Java will take care of anything “dangling” out there. That’s the benefit of a garbage-collected language.

    But what you do have is a StackOverFlow error. Basically, the stack (which is the chain of functions you’re in, and how deep that is) is much MUCH smaller than the heap. The heap is “more or less” the size of main memory. Each thread’s stack is much much smaller. Basically you’re reaching that limit by doing your “Depth” thing.

    If you want to test “leaks” (or the idea that you won’t have any eventually) try something more like this:

    public class MemoryLeakTest {
         int number=0;
         public MemoryLeakTest mobj;
    
         MemoryLeakTest(int num){
            number = num;
         }
    
         protected void finalize(){
            System.out.println(number + " released.");
         }
    
        public static void main(String[] args) {
            try{
                 System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
                 System.out.println("Free Memory in starting "+ Runtime.getRuntime().freeMemory());
                 MemoryLeakTest first = new MemoryLeakTest(0);  // Keep a reference to one of them
                 MemoryLeakTest current = first;
                 for(int i = 1; i < Int.Parse(args[0]); i++) // forgive me, Java's been a while.  This may be C#.  But parse the first arg for your number of objects
                 {
                     current.mobj = new MemoryLeakTest(i);
                     current = current.mobj;
                 }
                 System.out.println("Free Memory in end "+ Runtime.getRuntime().freeMemory());
                 System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
            }
            catch(Exception exp){}
            finally{
                System.out.println("Free Memory"+ Runtime.getRuntime().freeMemory());
                System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
            }
        }
    
    }
    

    That will give you a “chain” of objects all in memory until first goes out of scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample program where I have a class called ObserverTest where I
I am trying to build a network related program in Java. I have previous
I have this simple sample on VS2010: using System; namespace ConsoleApplication1 { class Program
i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
I tried to build a simple program in the terminal. #include <stdio.h> #include <stdlib.h>
I'm trying to build a simple program to price call options using the black
I've just build a sample application to test out google app engine and google
I am trying to build a sample application which will show a proof of
I was new to WCF, i was trying to build a sample application using
I am new to Android development and want to build a first sample application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.