How to optimize memory usage on a meta project building other projects?

I have a meta gradle project which is building other gradle projects, it clones does builds/releases for other project, it keeps throwing error

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"

even though in my i have

GRADLE_OPTS="-Xmx1024m -Xms256m -XX:MaxPermSize=512m -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError”

And i tried bumping up Xmx to 4 GB and still it keeps failing. Individual projects build separately just fine. Is there some way i can optimize grade’s memory usage ?

I am not sure how can i fix it,