Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it memory leak?
    primarykey
    data
    text
    <p>I build a sample program demonstrate memory leak in java.</p> <pre><code>public class MemoryLeakTest { static int depth = 0; int number=0; MemoryLeakTest mobj; MemoryLeakTest(){ number = depth; if(depth &lt; 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()); } } } </code></pre> <p>I run it by changing value of N in <code>if(depth &lt; N)</code>. An here is the result;</p> <p><strong>when depth is 1000</strong></p> <p>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)</p> <p><strong>when depth is 1500</strong></p> <p>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)</p> <p><strong>when depth is 6000</strong></p> <p>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)</p> <p><strong>when depth is 6500 (Exception in thread "main" java.lang.StackOverflowError)</strong></p> <p>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)</p> <p>My questions are;</p> <ol> <li>It is not calling finalize(). Is it memory leak?</li> <li>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?</li> <li>When N=6500, JVM generates error. So why last 2 statements of try{} are executed.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload