1

Sorry, I have a question about memory management.
I create a "user" object.
And when I end of using, I want to make it nil to lease memory.
I try it but I fail.
What's wrong with me?

var user:User? = User(userId)
user?.fromJson(data["user"])
self.users[userId] = user
user?.updateDb()
user = nil
Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
tolerate_Me_Thx
  • 387
  • 2
  • 7
  • 21
  • what do mean by "I fail"? do you get a compiler error or what? Btw. you shouldn't need to care about memory-management in swift, this language has a garbage collector, that will free memory if an object can not be accessed by your code anymore. check this: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html – JohnnyAW Aug 31 '17 at 07:14
  • @JohnnyAW Garbage Collection and ARC are two completely separate concepts. – mag_zbc Aug 31 '17 at 07:22
  • 2
    You have put your `User` in the `self.users` array; so there is a reference to it and it won't be released until that reference goes away. – Paulw11 Aug 31 '17 at 07:32

2 Answers2

1

If "User" is Reference Type

When you create object referenceCount incremented on 1

when you set user in Dictionary by "userId" referenceCount incremented on 1

and referenceCount == 2

after user = nil, you remove 1 reference

referenceCount == 1

and stay 1 strong reference, object is not removed

0

For the most part, you don't need to worry much about deinitializing an object. Swift, like it's predecessor Objective-C, uses Automatic Reference Counting (ARC) to keep track of which objects are still in use, and which need to be deleted. In short, if there is no strong reference to an object, it will automatically get deallocated.

In your case, you defined user local variable. If you don't store reference to that variable somewhere else, it will get deallocated immediately after you leave the scope in which it was declared.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • I'm just curious, how is this a "completely separate concept" to garbage collection? – JohnnyAW Aug 31 '17 at 07:34
  • @JohnnyAW [ARC vs. GC](https://docs.elementscompiler.com/Concepts/ARCvsGC/) –  Aug 31 '17 at 07:38
  • @ColGraff thx for the link, but this actually shows, that this are 2 different implementations to the same `concept` of memory-management – JohnnyAW Aug 31 '17 at 07:42
  • 1
    @JohnnyAW Yes, they both manage memory but they do so in very different ways. Garbage collection is more thorough and automatic but, in general, takes up more resources than automated reference counting. They are different concepts toward the same goal. –  Aug 31 '17 at 07:46
  • 1
    @mag_zbc Theoretically it could even be deallocated before the end of scope but that's an implementation detail. –  Aug 31 '17 at 07:47
  • @JohnnyAW ARC is an extension of manual Reference Counting from pre-iOS4. The memory management is still "manual", only now ARC inserts appropriate `retain`, `release` and `autorelease` calls at compile time and automatically generates `dealloc` methods. – mag_zbc Aug 31 '17 at 07:50