# Windows Hibernation Tutorial

## Introduction

*hibernation* is a Windows [system power states](https://learn.microsoft.com/en-us/windows/win32/power/system-power-states#hibernate-state-s4).
This tutorial describes how to safely enter hibernation, while running a QNN application, and provides code snippets demonstrating the QNN API
calls used to release system resources.

1. [Free context](https://docs.qualcomm.com/doc/80-63442-50/topic/hibernation.html#free-context)
2. [Free device](https://docs.qualcomm.com/doc/80-63442-50/topic/hibernation.html#free-device)
3. [Free backend](https://docs.qualcomm.com/doc/80-63442-50/topic/hibernation.html#free-backend)
4. [Close backend library](https://docs.qualcomm.com/doc/80-63442-50/topic/hibernation.html#close-backend-library)

### Steps for safe hibernation

## Free context

Any contexts created in a QNN backend should be released:

[Free context from SampleApp](https://docs.qualcomm.com/doc/80-63442-50/topic/sample_app.html#free-context)

1 if (QNN_CONTEXT_NO_ERROR !=
    2       m_qnnFunctionPointers.qnnInterface.contextFree(context, profileBackendHandle)) {
    3   QNN_ERROR("Could not free context");
    4   return StatusCode::FAILURE;
    5 }
    Copy to clipboard

## Free device

Devices created through the QNN API should be freed next:

1 if (QNN_DEVICE_NO_ERROR !=
    2       m_qnnFunctionPointers.qnnInterface.deviceFree(deviceHandle)) {
    3   QNN_ERROR("Failed to free device");
    4   return StatusCode::FAILURE;
    5 }
    Copy to clipboard

## Free backend

The application should then free any backends created:

[Free backend from SampleApp](https://docs.qualcomm.com/doc/80-63442-50/topic/sample_app.html#terminate-backend)

1 if (QNN_BACKEND_NO_ERROR !=
    2       m_qnnFunctionPointers.qnnInterface.backendFree(backendHandle)) {
    3   QNN_ERROR("Could not free backend");
    4   return StatusCode::FAILURE;
    5 }
    Copy to clipboard

## Close backend library

Finaly, the application should close the backend library:

[Loading a backend from SampleApp](https://docs.qualcomm.com/doc/80-63442-50/topic/sample_app.html#loading-a-backend) as shown below:

1 if (libBackendHandle) {
    2   pal::dynamicloading::dlClose(libBackendHandle);
    3 }
    Copy to clipboard

## Full SampleApp Hibernation Example

Now we can bring all the steps together using SampleApp as an example.

1 // Free context first
     2 if (QNN_CONTEXT_NO_ERROR !=
     3       m_qnnFunctionPointers.qnnInterface.contextFree(context, profileBackendHandle)) {
     4   QNN_ERROR("Could not free context");
     5   return StatusCode::FAILURE;
     6 }
     7
     8 // Free device if needed
     9 if (QNN_DEVICE_NO_ERROR !=
    10       m_qnnFunctionPointers.qnnInterface.deviceFree(deviceHandle)) {
    11   QNN_ERROR("Failed to free device");
    12   return StatusCode::FAILURE;
    13 }
    14
    15 // Free backend
    16 if (QNN_BACKEND_NO_ERROR !=
    17       m_qnnFunctionPointers.qnnInterface.backendFree(backendHandle)) {
    18   QNN_ERROR("Could not free backend");
    19   return StatusCode::FAILURE;
    20 }
    21
    22 // Close backhandle
    23 if (libBackendHandle) {
    24   pal::dynamicloading::dlClose(libBackendHandle);
    25 }
    26
    27 // This turns device into hibernation mode
    28 SetSuspendState(true, true, false);
    29 // Get back from hibernation
    30 return EXIT_SUCCESS;
    Copy to clipboard

Last Published: Oct 10, 2025

[Previous Topic
Windows](https://docs.qualcomm.com/bundle/publicresource/80-63442-50/topics/tutorials.md) [Next Topic
ARM64X Tutorial](https://docs.qualcomm.com/bundle/publicresource/80-63442-50/topics/arm64x_tutorial1.md)