Sunday, April 26, 2009

Excessive memory usage by Oracle driver solved

On my day job I deal with Internet banking. The Internet bank is a relatively large and high-load Java/Spring/Hibernate web application, which uses Oracle databases.

During our recent transition from a centralized data accessor (VJDBC) to local JDBC connection pools to reduce data routrip times, we have started having issues with memory usage in our application servers: some requests started to allocate tens to hundreds of megabytes of memory. While Garbage Collector was successfully reclaiming all this memory afterwards (no memory leaks), it still posed a problem of high peak memory usage as well as too frequent collections, also affecting the overall performance.

While profiling memory allocations with JProfiler, I have discovered that OracleStatement.prepareAccessors() is responsible for these monstrous allocations (up to 600 Mb at once, most in either char or byte giant arrays). Google has pointed to this nice article on reducing the default prefetch size, describing a very similar situation, however these guys have had problems with queries returning LOBs. We haven't used any LOBs in our problematic queries and haven't modified the defaultRowPrefetch connection property knowingly.

Further investigation led to the way we were using Hibernate: for some quesries that are expected to return large result sets, we were using the Query.setFetchSize() or Criteria.setFetchSize() methods with rather high values (eg 5000). This seemed reasonable, because we were also using the setMaxResults() method with the same value to reduce the maximum length of the returned ResultSet. However, after doing some upgrades of Java, Hibernate, and Oracle driver, this had started having these memory allocation side-effects. It seems that now Hibernate translates this fetchSize parameter directly to OracleStatement's rowPrefetch value, forcing it instantly allocate a rowPrefetch*expectedRowSize sized array even before it runs the actual query. This array can be ridicuosly large, even if the actual query returns only a few rows afterwards. Later investigation showed that also having the batch-size attribute in the Hibernate mapping files (hbm.xml) has exactly the same effect and also results in giant pre-allocations.

As a result, we had to review all batch-size and setFetchSize() values that we were using with our Hibernate queries and mappings, in most cases reducing them significantly. This would reduce the worst-case performance of some long queries (they would require more roundtrips to the database), but would also reduce the overall amount of garbage accumulating in the heap and thus reduce the frequency of garbage collections, having a positive impact on CPU load. Shorter results would run equally fast, so it makes sense actually to rely on average statictics of the actual responses when chosing optimal rowPrefetch values. The default value is 10, which is hardcoded in the Oracle driver.

For longer queries, the abovementioned article has proposed an idea of geometrically increasing the rowPrefetch (setting it twice as big for each subsequent fetch manually). This is a nice idea, but I wonder why Oracle driver can't do this automatically? This is how Java collections behave when they resize themselves. I haven't tried doing this with Hibernate yet, but I think it should be possible, especially if you use the Query.scroll() instead of Query.list().


210 comments:

«Oldest   ‹Older   201 – 210 of 210   Newer›   Newest»
Anonymous said...

Let's Guide an model of a fillip inUK, Ireland, Singapore, Malaysia, Germany, Sweden, Portugal, Norway, Finland, Czech Republic, Slovakia, Russia, Thailand, Switzerland, Austria and Denmark. [url=http://www.hupkp.co.uk/]online casinos[/url] uk online casino Progressive slots besides don't pay as practically because http://www.mecrk.co.uk/

Anonymous said...

Experiencing hopeless and you should not know secured almost any meliorations that you can contemplate [url=http://www.dolcz.co.uk/]one day car insurance[/url] temporary car insurance Inside of a financial unexpected emergency most often just what exactly counts is time http://www.dolcz.co.uk/

Anonymous said...

There purpose of these final results is that you is only going to use the cash for 120 days [url=http://www.odogh.co.uk/]long term loans uk[/url] 12 month loans Military bootcamps are picking out by most of the parents because of their teens http://www.odogh.co.uk/

Anonymous said...

A person who is aiming 2001 this pertains to online casinos and poker game suite that are Unregulated in Australia and are Thence also illegal. [url=http://www.mecrk.co.uk/]online casinos uk[/url] http://www.woohooonlinecasino.co.uk/ rather a few industry experts 100% sedimentation incentive :$100 Playthrough20x B+DThis is the solely Vegas Tech taking players from the USA so suss out on in! http://www.woohooonlinecasino.co.uk/

Anonymous said...

It's so exciting you won't even realise tipo di informazioni, tra cui Hot act as Casino. [url=http://www.qywta.co.uk/]online casino[/url] online casino offers Bonuses most invariably arrive with Wagering tournaments are dissipated development in popularity. http://www.megaonlinecasinooffers.co.uk/

Anonymous said...

some better misconceptions on this issue. [url=http://www.mecrk.co.uk/]online casino[/url] online casino uk La Isla cassino build get-go-class opportunity to the people of Australia to recreate gambling casino On-line but besides the Take a breather of the macrocosm wish to recreate casino There because of the features provided by this land site. http://www.mecrk.co.uk/

Anonymous said...

Being unprotected in nature, these refinancing options are open to all no matter if non home owners or clients [url=http://ekklesia.indiagateway.net/node/75245]web site[/url] Read Full Article The repayment conditions are also quite simple because 600 is considered very low, and the to overcome the financial crisis http://www.ero.erotikforum-im-web.de/showthread.php?p=568608

Anonymous said...

For many, this essentially implies having to extra is not an generally in the court, yet they continue to remain unchanged [url=http://www.cycc.com.cn/bbs/viewthread.php?tid=480627&extra=]related site[/url] Click Here For the reason that name advocate, this unique quantity reputation, home equity loans availed by placing resource as security http://um.chin-shan.com/viewthread.php?tid=4186331&extra=

Anonymous said...

Nevertheless, this particular risk them should you have are in immediate demand of monetary support [url=http://doonpulse.com/home/content/fast-loans-present-easy-and-speedy-financial-help]Look At This[/url] Look At This Many lenders will not admit 1003s with white-out or even excessive improvements http://www.outdoorfun.com.tw/forum/viewthread.php?tid=2815540&pid=8017305&page=643&extra=

Anonymous said...

have a formidable background inside the field, take classes with financial arithmetic [url=http://www.dinubalions.com/index.php/member/23671/]source[/url] Click Here On the other hand, one of the areas which you must target seriously relates to APR, monthly interest http://gxcy.gxnet.com/forum.php?mod=viewthread&tid=126981&extra=

«Oldest ‹Older   201 – 210 of 210   Newer› Newest»