memory leak in hello world demo!!!

Discussion in 'Java' started by giangiammy@gmail.com, Nov 15, 2005.

  1. Guest

    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);
    }
    }
     
    , Nov 15, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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?



    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);
    }
    }
     
    , Nov 15, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Nov 15, 2005
    #3
  4. wrote:
    > 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.
    >


    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.
     
    Tjerk Wolterink, Nov 15, 2005
    #4
  5. Alun Harford Guest

    <> wrote in message
    news:...
    > 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?


    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.*;
    >
    > 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);


    System.gc(); //Nasty bad evil line to force garbage collection.

    > }
    > }
    >
     
    Alun Harford, Nov 15, 2005
    #5
  6. Benji Guest

    Alun Harford, while chewing on bamboo shoots, wrote:
    > 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.

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
     
    Benji, Nov 15, 2005
    #6
  7. Alun Harford Guest

    "Benji" <> wrote in message
    news:dld51r$le3$...
    > Alun Harford, while chewing on bamboo shoots, wrote:
    > > 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.


    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
     
    Alun Harford, Nov 15, 2005
    #7
  8. Benji Guest

    Alun Harford, while chewing on bamboo shoots, wrote:
    > 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.

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
     
    Benji, Nov 15, 2005
    #8
  9. Roedy Green Guest

    On 15 Nov 2005 08:02:45 -0800, ""
    <> 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 15, 2005
    #9
  10. Roedy Green Guest

    Roedy Green, Nov 15, 2005
    #10
  11. Alun Harford Guest

    "Roedy Green" <> wrote in
    message news:...
    > On 15 Nov 2005 08:02:45 -0800, ""
    > <> 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


    Bad JVM. (But that's not the issue here)

    Alun Harford
     
    Alun Harford, Nov 15, 2005
    #11
  12. wrote:
    > 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?

    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.

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Nov 15, 2005
    #12
  13. . Guest

    On Tue, 15 Nov 2005 wrote:

    >
    > 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.


    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.

    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
     
    ., Nov 15, 2005
    #13
  14. . Guest

    On Tue, 15 Nov 2005, Alun Harford wrote:

    > <> wrote in message
    > news:...
    > > 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?

    >
    > 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.*;
    > >
    > > 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);

    >
    > 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.

    > > }
    > > }
    > >

    >
    >
    >


    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
     
    ., Nov 15, 2005
    #14
  15. Roedy Green Guest

    >> Alun Harford, while chewing on bamboo shoots, wrote:

    What the heck. Do you address strangers on the street that way? Why do
    you think it appropriate to do so on the net?

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 15, 2005
    #15
  16. Benji Guest

    Roedy Green, while chewing on bamboo shoots, wrote:
    >>> Alun Harford, while chewing on bamboo shoots, wrote:

    > 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?

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
     
    Benji, Nov 15, 2005
    #16
  17. Alun Harford Guest

    ""."" <> wrote in message
    news:p...
    > On Tue, 15 Nov 2005, Alun Harford wrote:
    >
    > > <> wrote in message
    > > news:...
    > > > 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?

    > >
    > > 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.*;
    > > >
    > > > 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);

    > >
    > > 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.


    Yes, sorry. I'm out-pedanted :)

    Alun Harford
     
    Alun Harford, Nov 15, 2005
    #17
  18. zero Guest

    "" <> wrote in
    news::

    > 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);
    > }
    > }
    >


    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.
     
    zero, Nov 16, 2005
    #18
  19. 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]...

    --
    Sometimes life just sucks and then you live.
     
    Thomas G. Marshall, Nov 16, 2005
    #19
  20. Roedy Green wrote:

    > 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.
     
    Andrew Thompson, Nov 16, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. F. GEIGER
    Replies:
    9
    Views:
    1,254
    F. GEIGER
    May 3, 2004
  2. vijay
    Replies:
    8
    Views:
    751
  3. Roy
    Replies:
    6
    Views:
    695
    Roedy Green
    Jan 7, 2008
  4. Larry
    Replies:
    27
    Views:
    484
    Michele Dondi
    Jan 25, 2005
  5. Tony Winslow
    Replies:
    4
    Views:
    116
    Tony Winslow
    Dec 31, 2007
Loading...

Share This Page