multithreading - Java using multiple threads during memory allocation -


i'm writing java program allocates large multi-dimensional array processing. during allocation, java ends using of cpus available (in case, 8 cpus). expected behaviour? i'm running ubuntu 14.04 java version 1.7.0_65. if need more information please let me know. if expected behaviour, there way limit uses 1 cpu?

public class test {     public static void main(string[] args){         system.out.println("test");          int = 1000;         int b = 100;         int c = 100;         int d = 1;         int e = 10;         float[][][][][] test = new float[a][b][c][d][e];           system.out.println("done");     } } 

edit: setting initial heap size large enough, program never ended using more 1 cpu. in future using both large enough initial jvm size , taskset command limit cpu affiinty.

allocation performed in current thread, if there insufficient space on heap allocation request can trigger garbage collection, which, depending on jvm , garbage collector tuning parameters, can employ multiple threads.

on oracle jvm, can verify indeed case supplying -verbose:gc option java process. on computer, program produces following output:

test [gc (allocation failure)  512k->456k(261632k), 0.0007027 secs] [gc (allocation failure)  968k->920k(261632k), 0.0014950 secs] [gc (allocation failure)  1432k->1452k(261632k), 0.0011264 secs] [gc (allocation failure)  1964k->1972k(262144k), 0.0009485 secs] [gc (allocation failure)  2996k->3020k(262144k), 0.0011456 secs] [gc (allocation failure)  4044k->4172k(264704k), 0.0014029 secs] [gc (allocation failure)  6220k->6124k(264704k), 0.0032563 secs] [gc (allocation failure)  8172k->8212k(267776k), 0.0032141 secs] [gc (allocation failure)  11796k->11836k(268288k), 0.0048205 secs] [gc (allocation failure)  15420k->15524k(271872k), 0.0048205 secs] [gc (allocation failure)  20644k->20772k(272896k), 0.0063642 secs] [gc (allocation failure)  25892k->25900k(275968k), 0.0066470 secs] [gc (allocation failure)  33580k->33636k(276480k), 0.0069722 secs] [gc (allocation failure)  41316k->41396k(281088k), 0.0105088 secs] [gc (allocation failure)  52660k->52564k(282112k), 0.0095962 secs] [gc (allocation failure)  63828k->64060k(293376k), 0.0127885 secs] [gc (allocation failure)  81468k->81380k(294400k), 0.0138240 secs] [gc (allocation failure)  98788k->99004k(307712k), 0.0192218 secs] [gc (allocation failure)  122044k->122196k(309248k), 0.0170214 secs] [gc (allocation failure)  145236k->145284k(318976k), 0.0242662 secs] [gc (allocation failure)  174468k->174620k(322048k), 0.0219776 secs] [gc (allocation failure)  203804k->203860k(334336k), 0.0287066 secs] [gc (allocation failure)  241236k->241356k(337920k), 0.0252992 secs] [gc (allocation failure)  278732k->278916k(354816k), 0.0359923 secs] [full gc (ergonomics)  278916k->277284k(400384k), 1.5867443 secs] [gc (allocation failure)  325412k->325695k(404992k), 0.0360435 secs] [full gc (ergonomics)  325695k->325411k(449024k), 0.7776627 secs] [gc (allocation failure)  373539k->373719k(449024k), 0.0383219 secs] [full gc (ergonomics)  373719k->373537k(493056k), 0.8760845 secs] [gc (allocation failure)  414497k->414792k(506368k), 0.0410957 secs] [full gc (ergonomics)  414792k->414495k(553472k), 0.9881370 secs] [gc (allocation failure)  455455k->455705k(549376k), 0.0421990 secs] [gc (allocation failure)  491545k->491545k(557568k), 0.0329242 secs] [full gc (ergonomics)  491545k->491290k(604160k), 1.1687245 secs] [gc (allocation failure)  527130k->527471k(621568k), 0.0546765 secs] [full gc (ergonomics)  527471k->527127k(676864k), 1.2818099 secs] [gc (allocation failure)  565527k->565750k(679424k), 0.0519078 secs] [gc (allocation failure)  604150k->604174k(669184k), 0.0404352 secs] [gc (allocation failure)  623630k->623910k(690176k), 0.0469939 secs] [full gc (ergonomics)  623910k->623379k(753152k), 1.6304653 secs] [gc (allocation failure)  642835k->643153k(751616k), 0.0642752 secs] [gc (allocation failure)  655441k->655473k(760832k), 0.0408013 secs] [gc (allocation failure)  667761k->667761k(760320k), 0.0448666 secs] [gc (allocation failure)  668273k->668273k(773632k), 0.0452070 secs] [gc (allocation failure)  668785k->668785k(784896k), 0.0464397 secs] [gc (allocation failure)  669297k->669297k(785408k), 0.0455053 secs] [gc (allocation failure)  669809k->669841k(799232k), 0.0461530 secs] [gc (allocation failure)  670353k->670401k(696320k), 0.0496845 secs] [full gc (ergonomics)  670401k->669956k(762880k), 1.6411686 secs] [gc (allocation failure)  670468k->670818k(855552k), 0.0602765 secs] [gc (allocation failure)  671842k->671778k(721408k), 0.0332749 secs] [gc (allocation failure)  672802k->672770k(846848k), 0.0333530 secs] [gc (allocation failure)  674306k->674426k(711680k), 0.0328486 secs] [full gc (ergonomics)  674426k->674045k(778240k), 0.6690573 secs] [gc (allocation failure)  675581k->675704k(913408k), 0.0583846 secs] [gc (allocation failure)  676216k->676184k(914944k), 0.0275571 secs] [gc (allocation failure)  676696k->676760k(912896k), 0.0276851 secs] [gc (allocation failure)  677784k->677768k(744448k), 0.0277325 secs] [gc (allocation failure)  678792k->678824k(903168k), 0.0281344 secs] [gc (allocation failure)  680360k->680360k(747520k), 0.0280448 secs] [gc (allocation failure)  681896k->681896k(889856k), 0.0294963 secs] [gc (allocation failure)  683944k->683912k(751616k), 0.0298778 secs] [gc (allocation failure)  685960k->685992k(874496k), 0.0305114 secs] [gc (allocation failure)  688552k->688616k(756736k), 0.0308301 secs] [gc (allocation failure)  691176k->691144k(860160k), 0.0315098 secs] [gc (allocation failure)  694728k->694696k(763904k), 0.0317261 secs] [gc (allocation failure)  698280k->698312k(844800k), 0.0324186 secs] [gc (allocation failure)  702920k->702952k(773120k), 0.0329357 secs] [gc (allocation failure)  707560k->707616k(834048k), 0.0336230 secs] [gc (allocation failure)  713248k->713264k(782336k), 0.0331354 secs] [gc (allocation failure)  718896k->718928k(835072k), 0.0336960 secs] [gc (allocation failure)  726096k->726200k(836608k), 0.0341606 secs] [gc (allocation failure)  733368k->733344k(836608k), 0.0368486 secs] [gc (allocation failure)  740000k->740064k(838656k), 0.0377613 secs] [gc (allocation failure)  746720k->746768k(839680k), 0.0393101 secs] [gc (allocation failure)  750864k->750864k(843776k), 0.0390566 secs] [gc (allocation failure)  754960k->755064k(845312k), 0.0392218 secs] [gc (allocation failure)  756600k->756640k(848896k), 0.0383347 secs] [gc (allocation failure)  758176k->758208k(850432k), 0.0379328 secs] [gc (allocation failure)  759232k->759200k(804864k), 0.0378035 secs] [gc (allocation failure)  760224k->760304k(846848k), 0.0400614 secs] [gc (allocation failure)  761840k->761848k(795136k), 0.0382733 secs] [gc (allocation failure)  763384k->763432k(835584k), 0.0364710 secs] [gc (allocation failure)  765480k->765568k(787968k), 0.0364749 secs] [gc (allocation failure)  767616k->767632k(834048k), 0.0357376 secs] [full gc (ergonomics)  767632k->766656k(881152k), 1.1594624 secs] [gc (allocation failure)  769216k->769444k(882688k), 0.0689485 secs] [gc (allocation failure)  772004k->772044k(884224k), 0.0334374 secs] [gc (allocation failure)  775116k->775188k(884224k), 0.0350246 secs] [gc (allocation failure)  778260k->778300k(881664k), 0.0336294 secs] [gc (allocation failure)  782396k->782396k(810496k), 0.0337357 secs] [gc (allocation failure)  786492k->786524k(877056k), 0.0349286 secs] [gc (allocation failure)  791644k->791684k(816640k), 0.0353728 secs] [gc (allocation failure)  796804k->796876k(869888k), 0.0359552 secs] [gc (allocation failure)  803020k->803068k(820736k), 0.0366515 secs] [full gc (ergonomics)  803068k->802478k(869888k), 1.8098995 secs] [gc (allocation failure)  808622k->808797k(913920k), 0.0726835 secs] [gc (allocation failure)  815965k->815997k(872448k), 0.0363674 secs] done 

as can see, jvm spends of time repeated attempts @ garbage collection. because jvm first attempt collect garbage before increasing heap size. can avoid instructing jvm start suitably large heap. again assuming oracle jvm, can accomplish supplying -xms1g on command line. then, output far shorter:

test [gc (allocation failure)  511k->496k(1048064k), 0.0007194 secs] [gc (allocation failure)  1008k->904k(1048064k), 0.0014771 secs] [gc (allocation failure)  1416k->1456k(1048064k), 0.0011328 secs] [gc (allocation failure)  1968k->1996k(1048576k), 0.0011072 secs] [gc (allocation failure)  3020k->3068k(1048576k), 0.0011904 secs] [gc (allocation failure)  4092k->4252k(1051136k), 0.0012557 secs] [gc (allocation failure)  6300k->6148k(1051136k), 0.0033741 secs] [gc (allocation failure)  8196k->8228k(1054208k), 0.0032742 secs] [gc (allocation failure)  11812k->11852k(1054720k), 0.0049498 secs] [gc (allocation failure)  15436k->15604k(1058304k), 0.0042598 secs] [gc (allocation failure)  20724k->20772k(1059328k), 0.0058598 secs] [gc (allocation failure)  25892k->25972k(1062400k), 0.0061824 secs] [gc (allocation failure)  33652k->33700k(1062912k), 0.0076966 secs] [gc (allocation failure)  41380k->41476k(1067520k), 0.0083110 secs] [gc (allocation failure)  52740k->52620k(1068544k), 0.0101210 secs] [gc (allocation failure)  63884k->64060k(1079808k), 0.0126912 secs] [gc (allocation failure)  81468k->81436k(1080832k), 0.0131405 secs] [gc (allocation failure)  98844k->99052k(1093120k), 0.0189248 secs] [gc (allocation failure)  121580k->121684k(1095168k), 0.0154816 secs] [gc (allocation failure)  144212k->144308k(1105408k), 0.0233011 secs] [gc (allocation failure)  173492k->173556k(1107968k), 0.0199322 secs] [gc (allocation failure)  202740k->202860k(1120768k), 0.0276851 secs] [gc (allocation failure)  240236k->240396k(1124352k), 0.0240986 secs] [gc (allocation failure)  277772k->277908k(1140736k), 0.0352077 secs] [gc (allocation failure)  326036k->326180k(1145344k), 0.0325568 secs] [gc (allocation failure)  374308k->374484k(1161728k), 0.0419046 secs] [gc (allocation failure)  431316k->431548k(1167872k), 0.0383821 secs] [gc (allocation failure)  488380k->488580k(1187328k), 0.0488973 secs] [gc (allocation failure)  556676k->556860k(1193472k), 0.0427763 secs] [gc (allocation failure)  624956k->625220k(1216000k), 0.0556787 secs] [gc (allocation failure)  706628k->706852k(1223168k), 0.0524774 secs] [gc (allocation failure)  788260k->788492k(1249792k), 0.0675712 secs] done 

in particular, there aren't major garbage collections, , program completes in fraction of cpu time.

of course, if create such large arrays repeatedly during course of program, garbage collection occur, , temporary use of other cores may still concern. in case, recommend tuning garbage collector through configuration options, somehow tricking jvm believing there less cores available, or limiting number of active jvm threads @ operating system level.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -