function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alextc78alextc78 

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:
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 literals
now 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!
Best Answer chosen by alextc78
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Alex,

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:
 
Map<String,List<String>> x;
x = new Map<String,List<String>>{'key1' => new List<String>{'a', 'b', 'c'}};
// stuff using x as list of literals
x = new Map<String,List<String>>{'key1' => new List<String>{'d', 'e', 'f'}};
// stuff using x as list of different literals
System.debug(x.values());    

// Will print: d, e, f

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Regards

All Answers

Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Alex,

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:
 
Map<String,List<String>> x;
x = new Map<String,List<String>>{'key1' => new List<String>{'a', 'b', 'c'}};
// stuff using x as list of literals
x = new Map<String,List<String>>{'key1' => new List<String>{'d', 'e', 'f'}};
// stuff using x as list of different literals
System.debug(x.values());    

// Will print: d, e, f

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Regards
This was selected as the best answer
alextc78alextc78
Thanks Adilson!
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
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Alex,

Don't worry. Salesforce has a top market memory management. Run this in excute anonymous window (developer console):
 
Map<String,List<String>> x;
x = new Map<String,List<String>>{'key1' => new List<String>{'a', 'b', 'c'}};
System.debug(Limits.getHeapSize());
// stuff using x as list of literals
x = new Map<String,List<String>>{'key1' => new List<String>{'d', 'e', 'f'}};
System.debug(Limits.getHeapSize());
x = new Map<String,List<String>>{'key1' => new List<String>{'d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}};
System.debug(Limits.getHeapSize());

You will get the following result:
17:56:38:002 USER_DEBUG [3]|DEBUG|1082
17:56:38:003 USER_DEBUG [6]|DEBUG|1082
17:56:38:003 USER_DEBUG [8]|DEBUG|1112

Regards.
alextc78alextc78
Awesome! thanks Adilson!
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".