Connecting Tech Pros Worldwide Forums | Help | Site Map

Memory Leaks (Java Vs. C)

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: May 14 '09
Expand|Select|Wrap|Line Numbers
  1. int *pi = .... //allocate n bytes
  2. pi = .... //again allocate m bytes (oops! n bytes lost)
  3. //can't be return this n bytes to OS till the computer shuts down. 
  4.  
Expand|Select|Wrap|Line Numbers
  1. String s = "";
  2. for(int i=0;i<n;i++) s += i;
  3.  
In first case it's obvious that memory leaks happens.
But in second case (Java) every time s is reassigning with a new reference.
Finally s will hold the new reference. And old reference no longer needed. Am I correct here? So the old references will get freed up when JVM runs garbage collection?

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 14 '09

re: Memory Leaks (Java Vs. C)


Quote:

Originally Posted by dmjpro View Post

So the old references will get freed up when JVM runs garbage collection?

Yes.

kind regards,

Jos

ps. the C version returns its memory when the process stops; not when the computer shuts down.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: May 14 '09

re: Memory Leaks (Java Vs. C)


Quote:

Originally Posted by JosAH View Post

ps. the C version returns its memory when the process stops; not when the computer shuts down.

Well, I disagree here ;)
When the program gets compiled and ready to start in native OS, then OS initializes the process block and etc ..what the process needs to run.
When the process claims a memory block then OS gives from it's own area which is shared by other process. Now when process ends then how do OS knows that memory blocked initialized by that particular process?

And this is what the difference between Java(run time system other than OS) and C or C++(runtime system OS itself). And in Java memory leaks is not concerned ;)
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: May 14 '09

re: Memory Leaks (Java Vs. C)


Quote:

Originally Posted by dmjpro View Post

Well, I disagree here ;)
When the program gets compiled and ready to start in native OS, then OS initializes the process block and etc ..what the process needs to run.
When the process claims a memory block then OS gives from it's own area which is shared by other process. Now when process ends then how do OS knows that memory blocked initialized by that particular process?

And this is what the difference between Java(run time system other than OS) and C or C++(runtime system OS itself). And in Java memory leaks is not concerned ;)

You are wrong; a block of memory claimed by the process belongs to the process and no other process can access it (*) (a segmentation violation will be the result) and the OS very well knows what memory belongs to which process. When the process finishes the OS reclaims all memory used by the process. Especially virtual memory handled by the hardware itself (memory management units) truly eases this task.

Even old MSDOS knew how to reclaim memory from died processes. Memory leaks are also a concern in Java: memory (objects) can still be reachable and thus won't be reclaimed by the garbage collector. Read the 'Weak References' article in the 'Insights' section.

kind regards,

Jos

(*) except for shared memory but it needs to be claimed as such.
Reply