> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Memory hooking

> Control PlayFab Services SDK memory allocations by installing custom alloc and free hooks with PFMemSetFunctions before PFInitialize.

# Managing Memory Allocations

With the C API in the PlayFab Services SDK, you can specify a function callback that the SDK calls whenever it tries to allocate memory. If you don't specify function callbacks, the SDK uses standard memory allocation routines.

To manually specify your memory routines, you can take the following steps:

* At the start of the game:
  * Call [**PFMemSetFunctions**](/services/playfab/api-references/c/pfplatform/functions/pfmemsetfunctions) to specify the allocation callbacks for assigning and releasing memory.
  * Call [**PFInitialize**](/services/playfab/api-references/c/pfcore/functions/pfinitialize) to initialize the PlayFab Core library.
  * Call [**PFServicesInitialize**](/services/playfab/api-references/c/pfservices/functions/pfservicesinitialize) to initialize the PlayFab Services library.

<Info>
  The order matters. **PFMemSetFunctions** must come before **PFInitialize**, and **PFInitialize** must come before **PFServicesInitialize**. If you skip the explicit **PFInitialize** call, **PFServicesInitialize** initializes Core internally—but at that point your memory hooks aren't applied yet, so the SDK uses default allocation routines instead.
</Info>

```cpp theme={null}
    PFMemoryHooks playFabHooks{ MyAllocHook, MyFreeHook };
    PFMemSetFunctions(&playFabHooks);
    HRESULT hr = PFInitialize(nullptr); // Add your own error handling when FAILED(hr) == true
    hr = PFServicesInitialize(nullptr); // Add your own error handling when FAILED(hr) == true
```

* While the game is running:
  * Any calls into the SDK that allocate or free memory call the specified memory handling callbacks.

* When the game exits:
  * Call [**PFServicesUninitializeAsync**](/services/playfab/api-references/c/pfservices/functions/pfservicesuninitializeasync) to reclaim all resources associated with PlayFab Services.
  * Call [**PFUninitializeAsync**](/services/playfab/api-references/c/pfcore/functions/pfuninitializeasync) to reclaim all resources associated with PlayFab Core.
  * Clean up your game's custom memory manager.

## Reference

[API reference documentation](/services/playfab/api-references/c/pfplatform/pfplatform_members)


## Related topics

- [XMemSetAllocationHooks](/reference/system/xmem/functions/xmemsetallocationhooks.md)
- [PFMemoryHooks](/services/playfab/api-references/c/pfplatform/structs/pfmemoryhooks.md)
- [Cross Core Memory Costs](/build/console-features/memory/Cross-Core-Memory-Costs.md)
- [Memory system for XBOX GDK titles](/build/console-features/memory/index.md)
- [Social Manager memory and performance](/services/xbox-services/community/social-manager/concepts/live-socmgr-mem-perf.md)
