# Memory Leak of Dictionary used in singleton

**URL:** <https://forums.swift.org/t/memory-leak-of-dictionary-used-in-singleton/5421>\
**Category:** Using Swift\
**Created:** [March 12, 2017, 12:04am UTC](https://forums.swift.org/t/memory-leak-of-dictionary-used-in-singleton/5421 "2017-03-12T00:04:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ekaterina\_Belinskaya](https://avatars.discourse-cdn.com/v4/letter/e/e19b73/32.png) [@Ekaterina\_Belinskaya](https://forums.swift.org/u/Ekaterina_Belinskaya)\
**Post date:** [March 12, 2017, 12:04am UTC](https://forums.swift.org/t/memory-leak-of-dictionary-used-in-singleton/5421/1 "2017-03-12T00:04:14Z")

</div>

Hi everyone!

I have singleton. It contains a 2 dictionaries.

struct Stat {  
&nbsp;&nbsp;&nbsp;&nbsp;var statHash:String  
&nbsp;&nbsp;&nbsp;&nbsp;var displayDescription:String  
&nbsp;&nbsp;&nbsp;&nbsp;var displayName:String  
&nbsp;&nbsp;&nbsp;&nbsp;var displayIcon:String  
&nbsp;&nbsp;&nbsp;&nbsp;var statIdentifier:String  
}

class Singleton {

&nbsp;&nbsp;&nbsp;&nbsp;static let sharedInstance = Singleton()

&nbsp;&nbsp;&nbsp;&nbsp;var statsDesc = [String:Stat]()  
&nbsp;&nbsp;&nbsp;&nbsp;var test = [String: String]()

&nbsp;&nbsp;&nbsp;&nbsp;init() {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test["a"] = "b"  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

let singlton = Singleton.sharedInstance  
On using the leaks tool, I am getting a memory leak of the second dictionary(String, String).

screenshot of memory graph debugger \<[http://imgur.com/a/JRcGd&gt](http://imgur.com/a/JRcGd&gt);

If i remove dictionaries and write smth like that:

class Singleton {  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;static let sharedInstance = Singleton()  
&nbsp;&nbsp;&nbsp;&nbsp;var num: Int  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;init() {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = 3  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}  
leak disappears.

Could someone, please, explain why this happens?

Thanks for the help.

Kate

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [March 13, 2017, 8:16am UTC](https://forums.swift.org/t/memory-leak-of-dictionary-used-in-singleton/5421/2 "2017-03-13T08:16:19Z")

</div>

In general a leak that you can reproduce with simple code snippets like this is Just a Bug™, and you should file it as such. It would help if you included a runnable test project that reproduces the problem.

\<[Issues · apple/swift · GitHub](https://bugs.swift.org/&gt);

Please post your bug number, just for the record.

Share and Enjoy

> **···**
>
> On 12 Mar 2017, at 00:04, Ekaterina Belinskaya via swift-users \<swift-users@swift.org\> wrote:
> 
> > Could someone, please, explain why this happens?
> 
> --  
> Quinn "The Eskimo!" \<[http://www.apple.com/developer/&gt](http://www.apple.com/developer/&gt);  
> Apple Developer Relations, Developer Technical Support, Core OS/Hardware

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 24, 2017, 8:48pm UTC](https://forums.swift.org/t/memory-leak-of-dictionary-used-in-singleton/5421/3 "2017-03-24T20:48:20Z")

</div>

This sounds a lot like:

[[SR-3661] Memory Leak from Multiple Dictionary Properties · Issue #46246 · apple/swift · GitHub](https://bugs.swift.org/browse/SR-3661) Memory Leak from Multiple Dictionary Properties

which was fixed in Xcode 8.3. What version of the compiler are you using?

-Joe

> **···**
>
> > On Mar 11, 2017, at 4:04 PM, Ekaterina Belinskaya via swift-users \<swift-users@swift.org\> wrote:
> > 
> > Hi everyone!
> > 
> > I have singleton. It contains a 2 dictionaries.
> > 
> > struct Stat {  
> > &nbsp;&nbsp;&nbsp;&nbsp;var statHash:String  
> > &nbsp;&nbsp;&nbsp;&nbsp;var displayDescription:String  
> > &nbsp;&nbsp;&nbsp;&nbsp;var displayName:String  
> > &nbsp;&nbsp;&nbsp;&nbsp;var displayIcon:String  
> > &nbsp;&nbsp;&nbsp;&nbsp;var statIdentifier:String  
> > }
> > 
> > class Singleton {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;static let sharedInstance = Singleton()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var statsDesc = [String:Stat]()  
> > &nbsp;&nbsp;&nbsp;&nbsp;var test = [String: String]()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test["a"] = "b"  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > let singlton = Singleton.sharedInstance  
> > On using the leaks tool, I am getting a memory leak of the second dictionary(String, String).
> > 
> > screenshot of memory graph debugger \<[http://imgur.com/a/JRcGd&gt](http://imgur.com/a/JRcGd&gt);
> > 
> > If i remove dictionaries and write smth like that:
> > 
> > class Singleton {  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;static let sharedInstance = Singleton()  
> > &nbsp;&nbsp;&nbsp;&nbsp;var num: Int  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;init() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = 3  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > leak disappears.
> > 
> > Could someone, please, explain why this happens?
