Carmine Lucarelli wrote:
> Hi all, trying to avoid possible memory leaks. When I instantiate objects in my
> servlet's service methods, will the references to those objects vanish when the
> doGet or doPost finishes, making them eligible for garbage collection?
Servlets are just Java classes -- they follow all of the same rules as any other
Java class.
Basically, if you instantiate an object and refer to it with a local variable
(inside your doGet()/doPost() method), and there are no other references to that
object when the doGet()/doPost() method returns, that object becomes garbage and
suitable for collection. If, on the other hand, you refer to it with an instance
variable of the servlet class, or store it in a user session, it is not subject to
garbage collection until the last reference is eliminated.
> Also, is
> there a method that would return how many objects
> are currently in existence?
>
There's no standard API calls to get the total number of allocated objects, but you
can get the current bytes of free and total memory in your JVM. Check out the
freeMemory() and totalMemory() methods of java.lang.Runtime.
>
> Thanks in advance,
>
> carmine
> [EMAIL PROTECTED]
>
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html