File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Java.lang.OutOfMemoryError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java.lang.OutOfMemoryError" Watch "Java.lang.OutOfMemoryError" New topic
Author

Java.lang.OutOfMemoryError

Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
Hello all,
I've got a servlet that creates 360 string variables and 4 mysql connections every time it runs. After 2 or 3 hours and about 70 to 100
runs tomcat needs to be shut down and restarted to clear the cache. I'm
finalizing and runing the garbage cllection at the end of the servlet but it doesn't seem to help. Does anyone have any suggestions or ideas? Any help is greatly appreciated.
Thanks,
Brian
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Are you closing the database connections?
Are you saving a lot of stuff in sessions?


[Jess in Action][AskingGoodQuestions]
Mark Latham
Greenhorn

Joined: May 27, 2003
Posts: 22
Though it's impossible to tell exactly where the problem is without a memory profiler, you're problem most likely lies in 1 of 3 areas:
  • The connections are not being explicitly closed
  • There is a member, static, or context-scoped variable that is keeping references to objects that would otherwise be garbage-collected
  • The sessions are not timing out quickly enough (due to server config)


  • You can address these common problem areas by explcitly closing all connections in the finally portion of a try/catch/finally block and by using only variables with local or session scopes (unless you have a really good reason).
    Also, please be aware that, due to the cost of creating database connections, you shouldn't be creating and destroying db connections every time a Servlet is executed. You should use a connection pool or, at the very least, hold onto the connections as member variables. If you do this, be sure any Statements or ResultSets are locally scoped and are explicitly closed in the finally block.
    And finally, don't explicitly call finalize and garbage collection methods. Just worry about closing your DB references and properly scoping your variables and the GarbageCollector will take care of the rest!
    [ December 01, 2003: Message edited by: Mark Latham ]
    Brian K Swingle
    Ranch Hand

    Joined: Jun 20, 2003
    Posts: 39
    I'm closing all the resultsets statments and connections, Should i be setting my 360 variables to Null in the finaly? Do you have a link to an example of connection pooling? Also do you think software like SpiritSoft's
    Jcache tool could help any with cleaning out the JVM heap?
    Thanks,
    Brian
    Mark Latham
    Greenhorn

    Joined: May 27, 2003
    Posts: 22
    You can find a sample ConnectionPool at Jakarta's Commons DBCP Project.
    You do not need to set your String variables to null in the finally block. As soon as they go out of scope, they will be marked as eligable for garbage collection. And you do not need a caching application (these may help you scale, but caching won't help with 70 - 100 sevlet executions over several hours).
    You do, however, need to consider your variable usage. 360 String variables is an unusually large number... What are you doing with them? Where are you declaring them (what is their scope)?
    And don't forget the session timeout. How long is your session timeout?
    Brian K Swingle
    Ranch Hand

    Joined: Jun 20, 2003
    Posts: 39
    Wow,
    thanks i've created an application based on a patented intellectual concept. That Will read your system information, Hardware Specs and so
    on and based on your PC's inforamtion the application will suggest processor upgrades that are compatible with your computer. we are currently
    also working on a memory, Hard drive, Video card and USB version of this app. Its sort of One click process to purchasing computer upgrades.
    "I'm not soliciting by any means but if you want to take it for a test drive the Beta of InSPECS can be found at my website. www.powerleap.com"
    The button is right on the home page.
    But anyway the 360 string variables are individual pieces of system information say one might be processor speed another might be Motherboard Model and so on. I use all these specs to determine information about your system and hence the possible upgrades for your system. The variables I have are all created just within the try. And i'm not currently using a Timeout do you have a small snippet of code that uses a Timeout. Thanks for all your help you guys have been great as usual.
    Thanks,
    Brian
    Brian K Swingle
    Ranch Hand

    Joined: Jun 20, 2003
    Posts: 39
    Now i'm really confused i was watching tomcat through SecureCRT and after a while i got a

    2003-12-02 15:09:35 - Ctx( ): Exception in: R( + /servlet/NewUpgradeFinder + null) - java.lang.OutOfMemoryError
    <<no stack trace available>>
    as usual but then the next 3 times the process ran perfectly with no error then it errored on the fourth. I'm totaly lost as to what is going on. i dont see any endless loops does anyone have any timeout snippets i could look at. oh do you think it might be a mysql cache problem?
    thanks,
    Brian
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Java.lang.OutOfMemoryError