views:

104

answers:

3

I have DB connection . the question is when I do the "next()" for getting data from DB, why the garbage collector runs simultaneoulsly. Cause of the GC , collectiing data from DB is so slow. How can I solve this out?

A: 

To test if it is really the garbage collector: increase heap space (-Xmx512m or even higher, depending on your actual settings), if it was the gc, there's a good chance that the problem is solved.

Andreas_D
+7  A: 

The garbage collector is running because something (in this case, probably the DB connection stack) is trying to allocate an object when there is insufficient free memory. This is normal behavior and you cannot entirely prevent it.

The first question is, is this really causing a significant slowdown? The minimal evidence you have presented is unconvincing. As comments point out, slow queries are not evidence of a GC-related problem, let alone that GC is causing the queries to be slow. Indeed, it could be the other way around. Poor query design could be the cause of the high level of GC activity; see point 3 below.

The second question is, why is this causing a noticeable slowdown? There are three possibilities:

  1. Simply your heap may be too small. The JVM works best, and GC is most efficient, if you can run with a big heap. If you run with a small heap, the GC runs more often and takes proportionally more time per object reclaimed. You can increase the heap size using the JVM options "-Xmx" and "-Xms"; look them up in the manual.

  2. Your application may have a memory leak. Over time, this will cause more and more of your heap to be filled with useless (but not garbage collectable) objects. As the heap approaches full, the GC will run more and more frequently, taking longer and longer. Eventually, the application will die with an OutOfMemoryError. The proper way to address this is to find and fix the memory leak. There are "bandaid" solutions such as increasing the heap, and using a certain JVM option to cause the JVM to exit when more than a given percentage of time is spent garbage collecting.

  3. Your application may just be generating huge numbers of objects as it fetches query results from the database. If this is the case, increasing the heap size will help a bit. However, you really need to look in detail at the DB queries you are performing to see if there's some way to reduce the amount of data that is retrieved from the database. Can you reduce it by doing more work on the DB side?

Finally, once you have addressed the three possibilities above, you may be able to improve things further by enabling the parallel GC. This will only help if you are running on a multi-core machine.

Stephen C
A: 

I think what you need to do firstly is find out the cause of the GC. Use some tool (like jconsole) to find out if something uses out the memory. If the momory consumption is expected, then you must increase your heap size. If this is an abnormal behavior. Use some profiling tool to find out where the memory leak happen. Considering you are doing some database operations, please double check if you forget to release some database resources (like connection) after using. After you solve the GC's problem, then go back to see if it's still very slow for "next()".

lostinmoney