java - Garbage collection when work is done with single or multiple threads -


i have jdbc program inserts around 50000 rows in table. have 2 version of program. first 1 insert 50000 rows using 1 thread while 2nd version inserts 50000 rows using 5 threads (each thread inserting 10000 rows).

now when profiling both program found garbage collection taking more cpu cycles when 5 threads used (44256 1 thread vs 401836 5 thread). profiler give me number of objects created each program same.

i wondering making gc take more cycle in multi threaded program. work done both program same (inserting 50000 rows) , number of objects same.

thanks manoj

every insert creates objects in jvm, must later garbage collected.

if perform inserts in parallel in multiple threads, it's higher "throughput" (inserts per second) if performed inserts sequentially.

background: because every individual insert, require round trip database, if inserts in parallel, thread can start insert before round trips have finished in others.

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

many garbage collectors generational. means use concurrent algorithms not pause vm reclaim memory pool of memory containing allocated (and discarded) objects - aka "eden space". eden space fixed in size. if pool fills faster concurrent algorithm can reclaim space, can trigger full garbage collection (full gc), pauses whole vm.

so long story short, might seeing higher gc overhead in multithreaded case, because multithreading giving higher throughput in program.

as side note: if want make program faster, take @ jdbc batch apis. these allow bulk-insert many rows in single round trip database.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -