[SalesForce] Heap size limit when uploading file using Visualforce page

I have a visualforce page which has control.
I am using this to upload a text file. I have property of type
Attachment in my controller. Value of inpufile control will be
Body of Attachment property.

In my controller action method, I am reading file contents as –

string attachmentBodyString = attachmentProperty.Body.toString();

I need to parse this string to do some business logic.
For larger files, I am running into Heap size limit error.

text file is around 1.5MB in size with around 15K lines.

I tried doing optimization wherever I can but still running into issues.

Can someone suggest any workaround for this ?

Best Answer

finally i optimized the code wherever i can. Got rid of temporary string variables and was able to make the code work for file size of 1.5 MB. It is now optimized to the point where it should be able to handle upto 3 to 4 MB file.