Apex is not the right tool for this, as it is not designed to handle such a large amount of CSV data (hence why you are getting an error).
I would suggest using Salesforce Data Loader if this job can be done manually. Otherwise something like a function in AWS Lambda could handle this use case.