Java Forum / General / November 2005
memory leak in hello world demo!!!
giangiammy@gmail.com - 15 Nov 2005 16:02 GMT hi all, I have a simple hello world program (at the end of the mail) I start it, move around, hide and puton forground again, and doing this I see a memory usage increments I'm using j2sdk1.4.2_06 on a linux Fedora Core 3 I look at: cat /proc/11383/status and I see the field VmRSS incrementing.
By what is caused this memory leak? where should I look to trace it?
thanks giammy
import java.awt.*; import java.awt.event.*;
public class HelloWorld extends Frame { public static void main(String[] args) throws Exception { final Frame frame = new HelloWorld(); frame.setSize(200, 200); frame.setVisible(true); }
public void paint(Graphics g) { g.drawString("Hello World", 10, 100); } }
geirgp@gmail.com - 15 Nov 2005 16:25 GMT > hi all, > I have a simple hello world program (at the end of the mail) [quoted text clipped - 7 lines] > By what is caused this memory leak? > where should I look to trace it? Without any testing I believe your problem is that the program keeps running after you close the frame. To prevent this from happening you must set default close operation on the frame, somethink like this (not tested so apologies if any typos):
public class HelloWorld extends Frame { public static void main(String[] args) throws Exception { final Frame frame = new HelloWorld(); frame.setSize(200, 200);
// exit program when frame is closed frame.setDefaultCloseOperation( Frame.EXIT_ON_CLOSE );
frame.setVisible(true); }
public void paint(Graphics g) { g.drawString("Hello World", 10, 100); } }
slippymississippi@yahoo.com - 15 Nov 2005 16:36 GMT There are no memory leaks in Java. If the memory continues to grow, it's because the native system requires the memory to perform the tasks you're requesting of it. Once your class loses scope, the Java virtual macine will reclaim that memory automatically.
I am trying to ween myself from C++, programming with which consumes much energy tracking memory usage. It sounds like you're in the same boat. Kick back and enjoy the fact that you'll never have to comb through memory usage with a fine-toothed comb ever again.
Tjerk Wolterink - 15 Nov 2005 16:55 GMT > There are no memory leaks in Java. If the memory continues to grow, > it's because the native system requires the memory to perform the tasks [quoted text clipped - 5 lines] > boat. Kick back and enjoy the fact that you'll never have to comb > through memory usage with a fine-toothed comb ever again. Wrong,
Memory tracking in Java is important... not as important as in c or c++, because we do not have to allocate/free memory.
But we must if we want an object to be garbage collected be sure that there is no reference to the object anymore.
For large programs there are still some references to unused objects: memory leak.
Roedy Green - 15 Nov 2005 17:45 GMT >Kick back and enjoy the fact that you'll never have to comb >through memory usage with a fine-toothed comb ever again. It is certainly easier, but you can't be quite that sanguine. See http://mindprod.com/jgloss/packratting.html and http://mindprod.com/jgloss/profiler.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
"." - 15 Nov 2005 18:26 GMT > There are no memory leaks in Java. If the memory continues to grow, > it's because the native system requires the memory to perform the tasks [quoted text clipped - 5 lines] > boat. Kick back and enjoy the fact that you'll never have to comb > through memory usage with a fine-toothed comb ever again. I strongly suggest you go to Google and enter "memory leaks java". You will find many articles on memory leaks and how to prevent them. You no longer have to concern yourself with explicitly calling free() or delete but you can still have problems.
 Signature Send e-mail to: darrell dot grainger at utoronto dot ca
Alun Harford - 15 Nov 2005 17:08 GMT > hi all, > I have a simple hello world program (at the end of the mail) [quoted text clipped - 7 lines] > By what is caused this memory leak? > where should I look to trace it? Sounds like the garbage collector hasn't bothered to collect the memory used, since there's loads of free space on the heap. The following would probably "solve" the "problem", but you'll take a large performance hit (and it's pointless).
> import java.awt.*; > import java.awt.event.*; [quoted text clipped - 8 lines] > public void paint(Graphics g) { > g.drawString("Hello World", 10, 100); System.gc(); //Nasty bad evil line to force garbage collection.
> } > } Benji - 15 Nov 2005 17:14 GMT > Sounds like the garbage collector hasn't bothered to collect the memory > used, since there's loads of free space on the heap. > The following would probably "solve" the "problem", but you'll take a large > performance hit (and it's pointless).
> System.gc(); //Nasty bad evil line to force garbage collection. I wouldn't even suggest this to him, since he obviously doesn't understand what's going on. There is no reason someone at his skill level should ever be forcing a garbage collection.
This is normal behavior. Java will reclaim memory when it needs to. If you are really worried about how much memory it's taking up for performance reasons, restrict the maximum heap size with the -Xmx parameter to the jvm; but if you just think that java has a "memory leak", then don't worry - your program doesn't.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Alun Harford - 15 Nov 2005 17:22 GMT > > Sounds like the garbage collector hasn't bothered to collect the memory > > used, since there's loads of free space on the heap. [quoted text clipped - 6 lines] > what's going on. There is no reason someone at his skill level should ever > be forcing a garbage collection. Well I did describe it as "nasty", "bad" and "evil". :-) I just put it down as I think it illustrates the point nicely.
(I'm guessing the OP isn't making commercial programs, so I don't see a problem with experimenting)
Alun Harford
Alun Harford
Roedy Green - 15 Nov 2005 19:08 GMT What the heck. Do you address strangers on the street that way? Why do you think it appropriate to do so on the net?
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Benji - 15 Nov 2005 20:15 GMT > What the heck. Do you address strangers on the street that way? Why do > you think it appropriate to do so on the net? It's a different form of communication. It's informal. Nobody could possibly be offended by that! What would there be to be offended by?
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Benji - 15 Nov 2005 17:25 GMT > Sounds like the garbage collector hasn't bothered to collect the memory > used, since there's loads of free space on the heap. > The following would probably "solve" the "problem", but you'll take a large > performance hit (and it's pointless).
> System.gc(); //Nasty bad evil line to force garbage collection. I wouldn't even suggest this to him, since he obviously doesn't understand what's going on. There is no reason someone at his skill level should ever be forcing a garbage collection.
This is normal behavior. Java will reclaim memory when it needs to. If you just think that java has a "memory leak", then don't worry - your program doesn't. It's possible, but only if you have references still around to data that you don't need. It's unlikely that in a simple GUI program, you'll have enough unnecessary references to affect anything.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
"." - 15 Nov 2005 18:29 GMT > > hi all, > > I have a simple hello world program (at the end of the mail) [quoted text clipped - 27 lines] > > System.gc(); //Nasty bad evil line to force garbage collection. The comment is incorrect. It should be:
// Nasty bad evil line to suggest the JVM run a garbage collection.
Note that it is a suggestion. You cannot FORCE a garbage collection.
> > } > > }
 Signature Send e-mail to: darrell dot grainger at utoronto dot ca
Alun Harford - 15 Nov 2005 21:12 GMT > > > hi all, > > > I have a simple hello world program (at the end of the mail) [quoted text clipped - 33 lines] > > Note that it is a suggestion. You cannot FORCE a garbage collection. Yes, sorry. I'm out-pedanted :-)
Alun Harford
Thomas G. Marshall - 16 Nov 2005 05:06 GMT Alun Harford coughed up:
...[rip]...
> Sounds like the garbage collector hasn't bothered to collect the memory > used, since there's loads of free space on the heap. > The following would probably "solve" the "problem", but you'll take a > large > performance hit (and it's pointless). ...[rip]...
> System.gc(); //Nasty bad evil line to force garbage collection. No, garbage collection in java cannot be "forced".
...[rip]...
 Signature Sometimes life just sucks and then you live.
Roedy Green - 15 Nov 2005 17:43 GMT On 15 Nov 2005 08:02:45 -0800, "giangiammy@gmail.com" <giangiammy@gmail.com> wrote, quoted or indirectly quoted someone who said :
>By what is caused this memory leak? >where should I look to trace it? I don't think you have a leak. As valid Java programs run, they create objects. Only when they run out of memory do they run garbage collection to get rid of objects no longer needed.
It is actually impossible to have a leak in Java, but you can packrat. See http://mindprod.com/jgloss/packratting.html
I see no sign of packratting in your program.
see http://mindprod.com/jgloss/garbagecollection.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Alun Harford - 15 Nov 2005 17:49 GMT > On 15 Nov 2005 08:02:45 -0800, "giangiammy@gmail.com" > <giangiammy@gmail.com> wrote, quoted or indirectly quoted someone who [quoted text clipped - 9 lines] > It is actually impossible to have a leak in Java, but you can packrat. > See http://mindprod.com/jgloss/packratting.html Bad JVM. (But that's not the issue here)
Alun Harford
Andrew Thompson - 16 Nov 2005 08:17 GMT > I don't think you have a leak. As valid Java programs run, they > create objects. Only when they run out of memory do they run garbage > collection to get rid of objects no longer needed. You can see a GUI based example of this in 'The Giffer'. <http://www.physci.org/giffer/giffer.jnlp>
It has an 'available memory' bar in the lower left. If you click* in any of the spinner controls on the right, the blinking cursor causes a gradual drop in memory.
Only when the VM thinks there is any reason, does it GC, (I never call GC from within the code).
Note that at times GC can be called at 20% (if the VM is especially idle), while more often it can drop as low as 3% or 1% before GC kicks in - and memory jumps back up to between 70% and 90%.
* Or do a variety of other things, the effect is particularly noticeable when encoding the animation.
Thomas Fritsch - 15 Nov 2005 17:58 GMT > I have a simple hello world program (at the end of the mail) > I start it, move around, hide and puton forground again, [quoted text clipped - 6 lines] > By what is caused this memory leak? > where should I look to trace it? What you described is not a memory leak. It is the normal memory behavior of a Java application. (1) It starts with little heap memory allocated (2) While running (and creating new objects) the allocated heap memory slowly grows (3) When heap memory reaches a certain threshold value, the garbarge collector (GC) frees as much unused objects as possible. The allocated heap memory then suddenly drops.
The sequence 1-2-3-1-2-3-... typically results in a "sawtooth"-like curve when drawing memory vs. time.
When you monitored your application, you happened to see only part of the first "sawtooth" of this curve, simply because you didn't wait long enough for a GC to occur.
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
zero - 16 Nov 2005 00:36 GMT > hi all, > I have a simple hello world program (at the end of the mail) [quoted text clipped - 25 lines] > } > } not related to your question, but when overriding a paint or paintComponent method, it's usually a good idea to call super.paint(g) resp. super.paintComponent(g) as first line in the overriding method.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|