title: SwiftUI - How to deinit a StateObject when navigating back?
body: <p>I want my <code>@StateObject</code> to be deinitialized as soon as possible after I navigate back, but it seems that the object is held in memory. &quot;Deint ViewModel&quot; is not being printed on back navigation, its first printed after I navigate again to the View I was coming from. Is there a way to release the <code>@StateObject</code> from memory on back navigation?</p>
  <pre><code>struct ContentView: View {
      var body: some View {
          NavigationView {
              NavigationLink(destination: TestView(), label: { Text(&quot;Show Test View&quot;) })
          }
      }
  }
  
  struct TestView: View {
      
      @StateObject private var viewModel = ViewModel()
      
      var body: some View {
          Text(&quot;Test View&quot;)
      }
  }
  
  final class ViewModel: ObservableObject {
      deinit {
          print(&quot;Deint ViewModel&quot;)
      }
  }
  </code></pre>