@@ -429,7 +429,9 @@ public AnalysisInfo buildAnalysisJobInfo(AnalyzeTableCommand command) {
429429 }
430430 infoBuilder .setTableVersion (version );
431431 infoBuilder .setPriority (JobPriority .MANUAL );
432- infoBuilder .setPartitionUpdateRows (tableStatsStatus == null ? null : tableStatsStatus .partitionUpdateRows );
432+ // Must not alias TableStatsMeta.partitionUpdateRows: it is cleared at the job terminal state.
433+ infoBuilder .setPartitionUpdateRows (tableStatsStatus == null ? null
434+ : new ConcurrentHashMap <>(tableStatsStatus .partitionUpdateRows ));
433435 infoBuilder .setEnablePartition (StatisticsUtil .enablePartitionAnalyze ());
434436 return infoBuilder .build ();
435437 }
@@ -548,6 +550,13 @@ public void updateTaskStatus(AnalysisInfo info, AnalysisState taskState, String
548550 if (MetricRepo .isInit ) {
549551 MetricRepo .COUNTER_STATISTICS_FAILED_ANALYZE_JOB .increase (1L );
550552 }
553+ // The job reached a terminal state: all tasks share the job's
554+ // partitionUpdateRows map, so clearing it here releases the memory
555+ // retained by every task record in the history at once. The success
556+ // path clears it inside updateTableStats.
557+ if (job .partitionUpdateRows != null ) {
558+ job .partitionUpdateRows .clear ();
559+ }
551560 } else {
552561 job .markFinished ();
553562 if (MetricRepo .isInit ) {
@@ -567,22 +576,27 @@ public void updateTaskStatus(AnalysisInfo info, AnalysisState taskState, String
567576
568577 @ VisibleForTesting
569578 public void updateTableStats (AnalysisInfo jobInfo ) {
570- TableIf tbl = StatisticsUtil .findTable (jobInfo .catalogId , jobInfo .dbId , jobInfo .tblId );
571- TableStatsMeta tableStats = findTableStatsStatus (tbl .getId ());
572- if (tableStats == null ) {
573- updateTableStatsStatus (new TableStatsMeta (jobInfo .rowCount , jobInfo , tbl ));
574- } else {
575- tableStats .update (jobInfo , tbl );
576- logCreateTableStats (tableStats );
577- }
578- if (jobInfo .jobColumns != null ) {
579- jobInfo .jobColumns .clear ();
580- }
581- if (jobInfo .partitionNames != null ) {
582- jobInfo .partitionNames .clear ();
583- }
584- if (jobInfo .partitionUpdateRows != null ) {
585- jobInfo .partitionUpdateRows .clear ();
579+ // Clear the shared maps even when the stats update fails, so the job and all its
580+ // task records release their memory once the job reaches a terminal state.
581+ try {
582+ TableIf tbl = StatisticsUtil .findTable (jobInfo .catalogId , jobInfo .dbId , jobInfo .tblId );
583+ TableStatsMeta tableStats = findTableStatsStatus (tbl .getId ());
584+ if (tableStats == null ) {
585+ updateTableStatsStatus (new TableStatsMeta (jobInfo .rowCount , jobInfo , tbl ));
586+ } else {
587+ tableStats .update (jobInfo , tbl );
588+ logCreateTableStats (tableStats );
589+ }
590+ } finally {
591+ if (jobInfo .jobColumns != null ) {
592+ jobInfo .jobColumns .clear ();
593+ }
594+ if (jobInfo .partitionNames != null ) {
595+ jobInfo .partitionNames .clear ();
596+ }
597+ if (jobInfo .partitionUpdateRows != null ) {
598+ jobInfo .partitionUpdateRows .clear ();
599+ }
586600 }
587601 }
588602
@@ -992,11 +1006,22 @@ public void handleKillAnalyzeJob(KillAnalyzeJobCommand killAnalyzeJobCommand) th
9921006 return ;
9931007 }
9941008 checkPriv (anyTask );
995- logKilled (analysisJobInfoMap .get (anyTask .getJobId ()));
1009+ AnalysisInfo job = analysisJobInfoMap .get (anyTask .getJobId ());
1010+ // The job record may have been evicted from analysisJobInfoMap while the job was
1011+ // still running; its shared partitionUpdateRows map was already cleared at
1012+ // eviction time (see replayCreateAnalysisJob), so skip the job-level update.
1013+ if (job != null ) {
1014+ logKilled (job );
1015+ }
9961016 for (BaseAnalysisTask taskInfo : analysisTaskMap .values ()) {
9971017 taskInfo .cancel ();
9981018 logKilled (taskInfo .info );
9991019 }
1020+ // The job reached a terminal state: all tasks share the job's partitionUpdateRows
1021+ // map, so clearing it here releases the memory retained by every task record.
1022+ if (job != null && job .partitionUpdateRows != null ) {
1023+ job .partitionUpdateRows .clear ();
1024+ }
10001025 }
10011026
10021027 private void logKilled (AnalysisInfo info ) {
@@ -1035,7 +1060,15 @@ private BaseAnalysisTask createTask(AnalysisInfo analysisInfo) throws DdlExcepti
10351060 public void replayCreateAnalysisJob (AnalysisInfo jobInfo ) {
10361061 synchronized (analysisJobInfoMap ) {
10371062 while (analysisJobInfoMap .size () >= Config .analyze_record_limit ) {
1038- analysisJobInfoMap .remove (analysisJobInfoMap .pollFirstEntry ().getKey ());
1063+ // pollFirstEntry removes the oldest entry from the map.
1064+ AnalysisInfo evicted = analysisJobInfoMap .pollFirstEntry ().getValue ();
1065+ // The evicted job may still be running: its updateTaskStatus will return
1066+ // early on the "job == null" guard and never reach the terminal-state
1067+ // clear, so clear the shared partitionUpdateRows map here to release the
1068+ // memory held by the evicted job and all its task records at once.
1069+ if (evicted .partitionUpdateRows != null ) {
1070+ evicted .partitionUpdateRows .clear ();
1071+ }
10391072 }
10401073 if (jobInfo .message != null && jobInfo .message .length () >= StatisticConstants .MSG_LEN_UPPER_BOUND ) {
10411074 jobInfo .message = jobInfo .message .substring (0 , StatisticConstants .MSG_LEN_UPPER_BOUND );
0 commit comments