If by increasing the memory limit you have gotten rid of the error and your code now works, you'll need to take measures to decrease that memory usage. Here are a few things you could do to decrease it:
If you're reading files, read them line-by-line instead of reading in the complete file into memory. Look at fgets and SplFileObject::fgets.
Upgrade to a new version of PHP if you're using PHP 5.3. PHP 5.4 and 5.5 use much less memory.
Avoid loading large datasets into in an array. Instead, go for processing smaller subsets of the larger dataset and, if necessary, persist your data into a database to relieve memory use.
Try the latest version or minor version of a third-party library (1.9.3 vs. your 1.8.2, for instance) and use whichever is more stable. Sometimes newer versions of libraries are written more efficiently.
If you have an uncommon or unstable PHP extension, try upgrading it. It might have a memory leak.
If you're dealing with large files and you simply can't read it line-by-line, try breaking the file into many smaller files and process those individually.
Disable PHP extensions that you don't need.
In the problem area, unset variables which contain large amounts of data and aren't required later in the code.
FROM: https://www.airpair.com/php/fatal-error-allowed-memory-size