Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java.lang.OutOfMemoryError

 
Brian K Swingle
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you closing the database connections?
Are you saving a lot of stuff in sessions?
 
Mark Latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic