#define MAX_CPUS 4096
 
-static u64 cpus_cstate_start_times[MAX_CPUS];
-static int cpus_cstate_state[MAX_CPUS];
-static u64 cpus_pstate_start_times[MAX_CPUS];
-static u64 cpus_pstate_state[MAX_CPUS];
+static u64 *cpus_cstate_start_times;
+static int *cpus_cstate_state;
+static u64 *cpus_pstate_start_times;
+static u64 *cpus_pstate_state;
 
 static int process_comm_event(struct perf_tool *tool,
                              union perf_event *event,
                "perf timechart record [<options>]",
                NULL
        };
+       int ret;
+
+       cpus_cstate_start_times = calloc(MAX_CPUS, sizeof(*cpus_cstate_start_times));
+       if (!cpus_cstate_start_times)
+               return -ENOMEM;
+       cpus_cstate_state = calloc(MAX_CPUS, sizeof(*cpus_cstate_state));
+       if (!cpus_cstate_state) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       cpus_pstate_start_times = calloc(MAX_CPUS, sizeof(*cpus_pstate_start_times));
+       if (!cpus_pstate_start_times) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       cpus_pstate_state = calloc(MAX_CPUS, sizeof(*cpus_pstate_state));
+       if (!cpus_pstate_state) {
+               ret = -ENOMEM;
+               goto out;
+       }
+
        argc = parse_options_subcommand(argc, argv, timechart_options, timechart_subcommands,
                        timechart_usage, PARSE_OPT_STOP_AT_NON_OPTION);
 
        if (tchart.power_only && tchart.tasks_only) {
                pr_err("-P and -T options cannot be used at the same time.\n");
-               return -1;
+               ret = -1;
+               goto out;
        }
 
        if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
 
                if (tchart.power_only && tchart.tasks_only) {
                        pr_err("-P and -T options cannot be used at the same time.\n");
-                       return -1;
+                       ret = -1;
+                       goto out;
                }
 
                if (tchart.io_only)
-                       return timechart__io_record(argc, argv);
+                       ret = timechart__io_record(argc, argv);
                else
-                       return timechart__record(&tchart, argc, argv);
+                       ret = timechart__record(&tchart, argc, argv);
+               goto out;
        } else if (argc)
                usage_with_options(timechart_usage, timechart_options);
 
        setup_pager();
 
-       return __cmd_timechart(&tchart, output_name);
+       ret = __cmd_timechart(&tchart, output_name);
+out:
+       zfree(&cpus_cstate_start_times);
+       zfree(&cpus_cstate_state);
+       zfree(&cpus_pstate_start_times);
+       zfree(&cpus_pstate_state);
+       return ret;
 }