---
title: 线程本地存储堆栈分配器示例
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/analysis/performance-memory/native-memory/allocator-examples/tls-stack-allocator
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/analysis/performance-memory/native-memory/allocator-examples/tls-stack-allocator.md
last_updated: 2026-10-03
generated_at: 2026-11-16T07:24:57.747Z
locale: zh-cn
---

# 线程本地存储堆栈分配器示例

> 检查 TLS 堆栈分配器分配器的使用情况 Reports。

## 先决条件##prerequisites

这些示例使用 Unity 在关闭 Player 或 Unity Editor When写入日志的Memory Usage情况Reports。要创建这些报告，请使用 `-log-memory-performance-stats` 命令行参数。要查找项目的日志文件，请遵循 [Log files Page](/engine/6000.0/manual/scripting/debugging-and-diagnostics/log-files.md) ON 的说明。

## TLS 堆栈分配器使用情况Reports##tls-stack-allocator-usage-reports

如果线程的堆栈分配器已满，分配将回退到[线程安全线性分配器](/engine/6000.0/manual/analysis/performance-memory/native-memory/allocator-examples/threadsafe-linear-allocator.md)。一些溢出分配可以：一帧 1 到 10 个，加载期间数百个。但是，如果数字帧都在增加，则可以增加代码块大小。


**Main Thread Block Size custom value in the Fast Per Thread Temporary Allocators:**
![](/api/media?file=/engine/6000.0/media/images/Per_Thread.png)

要增代码块大小，请在 Editor 中设置值，或使用命令行参数。有关更多信息，请参阅自[定义本机内存分配器](/engine/6000.0/manual/analysis/performance-memory/native-memory/memory-allocator-customization.md)。

使用上报中的信息可以帮助您选择适合应用程序程序的代码块大小。例如，在以下主要线程使用情况上报中，加载峰值为 2.7 MB，但剩余帧不足 64 KB。您可以代码块大小小从 4 MB 减小到 64 KB，并允许加载帧帧分配：

```lang-plain
[ALLOC_TEMP_TLS] TLS Allocator
  StackAllocators：
    [ALLOC_TEMP_MAIN]
      峰值使用帧数量：[16.0 KB-32.0 KB]：802 帧，[32.0 KB-64.0 KB]：424 帧，[2.0 MB - 4.0 MB]：1 帧
      初始始代代码块大小 4.0 MB
      当当前代码块大小 4.0 MB
      峰值分配字节 2.7 MB
      溢出数量 0
    [ALLOC_TEMP_Job.Worker 18]
```

在以下示例中，工作线程不用于大型临时分配。为了Save内存，可将工作线程的代码块大小小减少到 32 KB。ON 多核机器很有用，因为多核机器中的每个工作线程都有自己的堆栈：

```lang-plain
[ALLOC_TEMP_Job.Worker 14]
      初始始代代码块大小 256.0 KB
      当当前代码块大小 256.0 KB
      峰值分配字节 18.6 KB
      溢出数量 0
```

## 其他资源##additional-resources

* [本机内存分配器](/engine/6000.0/manual/analysis/performance-memory/native-memory/allocators.md)
* [本机内存分配器示例](/engine/6000.0/manual/analysis/performance-memory/native-memory/allocator-examples.md)
