Reputation: 2315
In swift, strings are struct types, so the values are going to be stored in stack memory rather than the heap.
Lets say we declare a string:
let a = ""
let b = "o"
This is an empty string, but some memory is allocated. I need to know what is the difference in the memory given to an empty string and a string with let's say count 1.
If we say:-
var str:String?
var str: String = ""
The difference in memory layout by drawing is what I need to see. (stack / heap).
I tried to find this on google but nowhere I can't find the memory layout for this.
Upvotes: 14
Views: 2549
Reputation: 80951
String
is a very complicated beast. It has many different underlying representations, including:
String literals, which are stored as constants in the binary
Small strings, which can store up to 15 UTF-8 code units inline on the stack (on x86 at least)
"Native" strings (i.e strings that haven't been bridged to Objective-C), which are stored in a tail-allocated heap buffer
"Foreign" strings from Obj-C which can be represented by a tagged pointer or heap allocated NSString
object
"Shared" strings which can represent a Swift string literal as a heap allocated Obj-C object
If you want to dive into the nitty gritty details, you can start at StringObject.swift, which has a bunch of comments explaining some of the layout details.
To answer your question about the examples you give:
let a = ""
An empty string has a canonical small string representation which is stored inline on the stack.
let b = "o"
A string literal is stored as a constant in the binary. Therefore no heap allocation is required – a pointer to the string's address in the binary is all that's required.
But even if this wasn't a constant string, for example it was created by:
let o = "O".lowercased()
Then it can still be represented as a small string, which is stored inline on the stack.
var str: String?
String
has an extra inhabitant (at least on x86), meaning that String?
can share the same layout as String
. The value nil
is represented by one of the extra inhabitant bit patterns.
But even if String
didn't have an extra inhabitant, no additional heap allocation would be required to represent String?
over String
– it would only require an additional bit of inline storage.
Upvotes: 16
Reputation: 25
As we know Strings is value type in swift and value type variable store in stack memory allocation.
var str:String?
var str1: String = ""
in above case both consume stack memory. in str can assign nil value but in str1 you never assign nil value.
Upvotes: -3
Reputation: 2264
As far I know strings
in swift have value type semantics with lots of optimizations (copy on write etc), but under hood they are bridged to NSString
class so they are allocated on heap.
Value type semantics means that from swift programmer perspective they are copied (with copy on write optimization) when we assign value to another variable or pass to method, we can mutate value inside method, and source string will not be affected, let keyword disallow mutations (checked by compiler) and so on.
I would recommend for you watch Mike Ash Exploring Swift Memory Layout presentation from GOTO 2016 conference.
Upvotes: -2