dcsimg
<font color=red>what is memory leaking </font>
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Monday, August 5, 2002 03:22 AM

Hello Could you pls tell me what is memory leaking and Why does it occurs.? Suppose that I have a servlet application that send mail with attachment. The program contains 2 clasees. 1. index.class- a servlet that accepts "email" from user 2. Mail.class- a servlet that do mailing function. ------------------------------------------------------ let us examine how this work The user input email using index.class. After validating the user request the index.class calls "sendmail method" of Mail.class . The sendmail method connects to databas   More>>

Hello



Could you pls tell me what is memory leaking and Why does it occurs.?

Suppose that I have a servlet application that send mail with attachment.

The program contains 2 clasees.



1. index.class- a servlet that accepts "email" from user


2. Mail.class- a servlet that do mailing function.





------------------------------------------------------



let us examine how this work


The user input email using index.class. After validating the user request the

index.class calls "sendmail method" of Mail.class .



The sendmail method connects to database and fetches some data and stores in
a ByteArrayOutputStream.

The program attach this content to mail and send to the email address.



When the program calls the sendmail method a new object of ByteArrayOutputStream
is created with every request.



eg


public void sendMail()

{

ByteArrayOutputStream stream= new ByteArrayOutputStream();

}




The amount of data that will be stored in ByteArrayOutputStream will be about 1 mb.

so if there are 10 request; 10 ByteArrayOutputStream instances are created.


After the program finishes the grarbage collector collects the unused objects and frees the memory



But consider grarbage collector only work when the server is idle.If the above class is
frequenty used the the memory will be filled with unused objects[instances of ByteArrayOutputStream with a mimmuk data of 1 mb]




Could you pls tell me how can I handle such situation? By assigning a null value to the

ByteArrayOutputStream instance will help to free the memory?



if a code change will help?if so pls help me to do the change.


Also how can I detect that there is a "memory leaking" in the program.

Is it possible to find what object causes the problem?




Thanks in advance


sabu

   <<Less

Re: what is memory leaking

Posted By:   Joe_Cheng  
Posted On:   Tuesday, August 6, 2002 09:32 AM

I wouldn't worry about it.

"But consider grarbage collector only work when the server is idle." That's technically true, but in practice, if this is happening through a servlet you're almost guaranteed that the garbage collection will happen very soon after the object goes out of scope. It's just the nature of web applications; even the busiest sites have some latency between requests.

Also, I don't think it's possible that Java will throw an OutOfMemoryError while there are obsolete objects in memory. In other words, the garbage collector WILL run to make sure you don't run out of memory. It just might slow you down a little if the garbage collection happens at an inopportune time.

Re: <font color=red>what is memory leaking </font>

Posted By:   han_jian  
Posted On:   Monday, August 5, 2002 06:02 PM

you can deal with this problem by using JProbe. With this tool, u can identify those unused object.

Re: <font color=red>what is memory leaking </font>

Posted By:   Anonymous  
Posted On:   Monday, August 5, 2002 07:04 AM

The stream variable, goes out of scope when the sendMail() method exits. The variable is then ready to be garbage collected. setting it to null will not make a difference.

You could use System.gc(), but this will not garantee that the garbage collector is run. It's only a 'hint' to the JVM.

Also, running garbage collecting to often will break performance.


Here is an article about Java and 'memory-leaking'.

It's about problems similar to yours.

Read more here

You can also configure garbage collection a lot.

This article is about garbage collection performance tuning:

Performance Tuning
About | Sitemap | Contact