Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty Swift string Memory allocation Layout (Stack/ Heap allocation)

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.

like image 538
Sudhanshu Gupta Avatar asked Dec 20 '18 08:12

Sudhanshu Gupta


People also ask

How does Swift allocate memory?

Generally speaking, Swift has 2 fundamental parts for memory allocations. Keep in mind that Swift automatically allocates memory in either the heap or the stack. 1. Memory that lives in the heap 2. Example of code that gets stored in the heap 3. When the heap is used 1. Memory that lives in the stack 2. Example of code that gets stored in the stack

What is heap memory allocation?

Memory allocated in the heap is often referred to as dynamic memory allocation. In contrast with stack memory, it’s the programmer’s job to allocate and deallocate memory in the heap. You can think of heap memory as a chunk of memory available to the programmer. The heap memory size depends on the size of virtual memory available to the program.

What are heaps and stacks in Swift?

The ways in which heaps and stacks are used in Swift to store memory have a lot of similarity with how we use data structures. In the next article, we’re going to see what these data structures do and how both are implemented in Swift!

How is memory allocated in C++ and Java?

Memory in a C/C++/Java program can either be allocated on a stack or a heap. Prerequisite: Memory layout of C program. Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack.


1 Answers

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.

like image 90
Hamish Avatar answered Sep 23 '22 05:09

Hamish