memory leak in hello world demo!!!

G

giangiammy

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

geirgp

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

slippymississippi

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

Tjerk Wolterink

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

Alun Harford

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

Benji

Alun said:
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.
 
A

Alun Harford

Benji said:
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
 
B

Benji

Alun said:
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.
 
R

Roedy Green

A

Alun Harford

Roedy Green said:
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
 
T

Thomas Fritsch

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

.

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

.

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

Roedy Green

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

Benji

Roedy said:
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?
 
A

Alun Harford

"." said:
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
 
Z

zero

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

Thomas G. Marshall

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

Andrew Thompson

Roedy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top