I need to optimize my code in terms of memory, but I don't know which part of the code and how
"What part of the code" is a really good question to start with, and "how" really depends on it.
Most of the time it is unreasonable to try optimizing everything (may end up optimizing 1% of memory while 90% of it is accidentally eaten by some not-fixed bug).
I recommend finding a memory profiler that suits your needs. For example, if you can use Valgrind it has "massif" memory profiler included.
Valgrind also includes tools to check for memory leaks (when you allocate memory and never clean it), which may contribute to high memory consumption as well.
https://postimg.cc/v1rp7S7H
Here's an example from massif-visualizer that I've just run on one of my tests, I could quickly see that I have some render data that eats up most of the space, and I know that's a bug because render should be disabled so this data shouldn't even be there.
Your case may be more complex than this, but having a good tool for this in your toolbox may make it much easier to reason about these problems.