You need to sign in to do that
Don't have an account?
alextc78
instantiate same object multiple times
Hi,
I'm trying to use a map as a static set of values, something akin to a literal, and want to use different values for the 'literal' in different sections of code. What I would like to do is something like this:
I could just create multiple Maps, but it seems unnecessary when each of these sets are used mutually exclusively, and i have a perfectly good Map sitting there already.
thanks in advance!
I'm trying to use a map as a static set of values, something akin to a literal, and want to use different values for the 'literal' in different sections of code. What I would like to do is something like this:
Map<String,List<String>> x; x = new Map<List<String>>{'key1' => new List<String>{'a', 'b', 'c'}, etc}; // stuff using x as list of literals x = new Map<List<String>>{'key1' => new List<String>{'d', 'e', 'f'}, etc}; // stuff using x as list of different literalsnow my question is how this will behave in apex - whether this will create a memory leak? when I instantiate the same map the second time, does the memory get freed from my first 'instance'? I haven't coded in 18 years, and that was in C where I had to be more explicit with my memory.
I could just create multiple Maps, but it seems unnecessary when each of these sets are used mutually exclusively, and i have a perfectly good Map sitting there already.
thanks in advance!
Your second X assignment will replace the first one. If you debug the values of x you will get d, e, f. Check out this code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Regards
All Answers
Your second X assignment will replace the first one. If you debug the values of x you will get d, e, f. Check out this code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Regards
just to be clear, so the previous instantiation memory is freed? I know the values will be replaced and the code will work (since I tested it), but I wasn't sure if I was creating a stealth memory leak that would plague me if I scaled it up.
cheers
Don't worry. Salesforce has a top market memory management. Run this in excute anonymous window (developer console):
You will get the following result:
Regards.
Limits.getHeapSize() is useful to remember, thanks for the tip. will have to look into the Limits class to try to understand Apex a bit better.
I've noticed that programming has come a long way since back in my day.
I was amazed the other day by delinters and "beautify".