简体   繁体   中英

Garbage collection when work is done with single or multiple threads

I have a JDBC program which inserts around 50000 rows in a table. I have 2 version of this program. First one insert 50000 rows using one thread while 2nd version inserts 50000 rows using 5 threads (each thread inserting 10000 rows).

Now when I do profiling for both the program I found that Garbage Collection is taking more CPU cycles when 5 threads are used (44256 for one thread Vs 401836 for 5 thread). Profiler also give me that number of objects created by each program are almost the same.

I am wondering what is making GC to take more cycle in multi threaded program. The work done by both the program is same (inserting 50000 rows) and even the number of objects are also same.

Thanks Manoj

Every insert probably creates objects in the JVM, which must later be garbage collected.

If you perform inserts in parallel in multiple threads, then it's likely that you will get higher "throughput" (inserts per second) than you would if you performed inserts sequentially.

Background: This is because every individual insert, will require a round trip to the database, but if you do inserts in parallel, then another thread can start an insert before the round trips have finished in the others.

The multithreaded case probably creates more garbage objects in the same amount of time than the single threaded case.

Many garbage collectors are generational . This means that they use concurrent algorithms which do not pause the VM to reclaim the memory from the pool of memory containing the most recently allocated (and discarded) objects - aka "eden space". Eden space is often fixed in size. If this pool fills up faster than the concurrent algorithm can reclaim space, then it can trigger a full garbage collection (full GC), which pauses the whole VM.

So long story short, you might be seeing higher GC overhead in the multithreaded case, because multithreading is giving you higher throughput in your program.

As a side note: if you want to make your program faster, take a look at the JDBC batch APIs . These allow you to bulk-insert many rows in a single round trip to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM