# 运行时系统

运行时系统（常简称为运行时）是辅助执行、或在某些设计中实际执行用某种编程语言编写的程序的软件；它是该语言执行模型的一部分，提供内存管理、动态链接、异常处理和线程调度等服务，这些服务是编译或解释后的程序单凭自身无法提供的<sup>[1](https://www.cs.uaf.edu/~chappell/class/2026_spr/cs331/read/comp_int.html)</sup><sup> • </sup><sup>[2](https://learnlyai.co.uk/en/course-hub/computer-science/programming-languages-paradigms/compilers-and-interpreters/runtime-systems-and-memory-management)</sup>。按一种宽泛的定义，基本上每种语言都有运行时系统，编译型语言和解释型语言皆然<sup>[3](https://www.lb7.uscourts.gov/documents/13c78953.pdf)</sup>。典型的 C 执行环境使用的运行时相对有限，由启动代码、标准库、栈展开函数和算术辅助函数组成，而 Java 和 C# 等语言使用的运行时则大得多，围绕虚拟机构建<sup>[4](https://techpedia.wiki/en/wiki/Programming_language)</sup><sup> • </sup><sup>[5](https://apt.cs.manchester.ac.uk/ftp/pub/apt/theses/Clarkson18_phd.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 辅助、有时实际执行程序的代码；语言执行模型的一部分<sup>[1](https://www.cs.uaf.edu/~chappell/class/2026_spr/cs331/read/comp_int.html)</sup> |
| 与操作系统的关系 | 除非使用虚拟化，一台机器只有一个操作系统，但可以有多个运行时系统，每个运行时系统都为其程序访问文件等操作系统服务充当中介<sup>[6](https://www.mccormick.northwestern.edu/computer-science/documents/tech-reports/2016/2016-12-hybrid-runtime-systems.pdf)</sup> |
| 核心服务 | 垃圾回收、即时编译、类加载、异常处理和线程调度（例如在 JVM 运行时中）<sup>[2](https://learnlyai.co.uk/en/course-hub/computer-science/programming-languages-paradigms/compilers-and-interpreters/runtime-systems-and-memory-management)</sup> |
| 托管运行时速度 | 在 LangBench 对比中，Go 和 OpenJDK 的平均运行耗时分别为 GCC 的 1.30 倍和 1.43 倍；V8/Node.js 和 CPython 则分别为 8.01 倍和 29.50 倍<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup> |
| 启动时间至关重要 | 2020 年 AWS Lambda 调用的中位运行时间为 60 毫秒，而 JVM 和 V8 的启动时间在数百毫秒或数秒量级<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup> |
| WebAssembly 运行时 | Wasmtime 是构建在 Cranelift 代码生成器上的独立 Wasm 运行时，可在 5 微秒内创建新的 Wasm 实例<sup>[8](https://bytecodealliance.org/articles/wasmtime-portability)</sup> |

## 运行时系统做什么

运行时系统是支持程序执行的服务与数据结构的集合。它的职责取决于语言承诺了什么。在具有自动内存管理的语言中，运行时包含垃圾回收器，负责识别并回收不再可达的内存<sup>[9](https://www.shreesathyam.edu.in/files/CM/CSE/5TH/CS3501/CS3501-NOTES.pdf)</sup>。在动态类型语言中，运行时必须在运行期确定对象的类型，并且通常在执行期间进行动态类型检查和边界检查<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup>。当程序读取文件或打印文本时，它通常调用运行时库中的函数，该函数再与操作系统交互以完成操作<sup>[10](https://www.bitminds.org/the-foundations-and-evolution-of-runtime-systems/)</sup>。

**一个操作系统，多个运行时。** 一种有用的定位方式是按数量来看运行时系统在软件栈中的位置：除非使用虚拟化，一台机器上只有一个操作系统，但可以有多个运行时系统。操作系统和运行时都根据程序中的调用发起系统调用，为程序与机器的交互充当中介，例如打开文件<sup>[6](https://www.mccormick.northwestern.edu/computer-science/documents/tech-reports/2016/2016-12-hybrid-runtime-systems.pdf)</sup>。

**动态链接。** 在运行期加载和绑定代码是常见的运行时服务。在 Windows 上，运行期动态链接使用 LoadLibrary 或 LoadLibraryEx 将 DLL 映射到进程地址空间，使用 GetProcAddress 获取导出函数的地址；FreeLibrary 递减模块引用计数，并在计数归零时取消 DLL 的映射。这种模式让进程即使某个 DLL 不可用也能继续运行，例如尝试另一个 DLL 或通知用户，这与加载期链接形成对比——在加载期链接中，如果系统找不到 DLL，就会直接终止进程<sup>[11](https://learn.microsoft.com/en-us/windows/win32/dlls/run-time-dynamic-linking)</sup>。Java 虚拟机在每个栈帧内执行类似的服务：动态链接将类文件中的符号方法引用转换为具体的方法引用，必要时加载类，这种延迟绑定使其他类的变更不太可能破坏代码<sup>[12](https://docs.oracle.com/javase/specs/jvms/se26/html/jvms-2.html)</sup>。

## 托管运行时与虚拟机

许多现代语言运行在虚拟机上，虚拟机要么将虚拟指令集（字节码）、要么将程序源代码针对底层机器进行动态翻译和优化；虚拟机的组件和服务共同构成所谓的托管运行时系统<sup>[13](https://www.cs.cmu.edu/~wasm/cs17-670/fall2022/)</sup>。编程语言的虚拟机在概念上不同于语言的运行时系统，尽管运行时（环境）和虚拟机这两个术语经常互换使用<sup>[14](https://citeseerx.ist.psu.edu/document?doi=05db5fc48b7d4aef0ae397f21dd7b52ae18d63ef&repid=rep1&type=pdf)</sup>。

**JVM。** Java 虚拟机定义了执行期间使用的运行时数据区：每个线程的 pc 寄存器和 Java 虚拟机栈，以及堆、方法区、运行时常量池和本地方法栈，这些区域按规范共享或分配。堆在虚拟机启动时创建，是所有类实例和数组的内存分配区域，堆存储由称为垃圾回收器的自动存储管理系统回收；对象从不显式释放<sup>[15](https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html)</sup>。Sun 术语表将 Java 运行时系统描述为包括加载用 Java 编写的程序所需的所有代码、动态链接本地方法的代码、内存管理、异常处理以及虚拟机的实现（可能是 Java 解释器）<sup>[16](https://docs.oracle.com/cd/E19957-01/805-4368/6j450e60s/index.html)</sup>。新的运行时特性继续在这一层出现：JDK 21 中交付的虚拟线程是由 JDK 而非操作系统提供的轻量级线程，其栈以栈块对象的形式存储在受垃圾回收的堆中<sup>[17](https://openjdk.org/jeps/444)</sup>。

**CLR。** .NET 的[公共语言运行时](https://www.edgepedia.org/zh/common-language-runtime)负责运行代码并提供使开发更简便的服务；面向它的编译器生成的代码称为托管代码，享有跨语言集成、跨语言异常处理、增强的安全性、版本控制和部署支持以及调试和性能分析服务。编译器生成描述程序类型、成员和引用的元数据，这些元数据与代码一起存放在每个可加载的可移植可执行文件中。运行时从中查找和加载类、在内存中安排实例、解析方法调用、生成本地代码、实施安全策略并建立运行时上下文边界。类型系统、元数据格式和虚拟执行系统由公开的 ECMA 公共语言基础设施规范定义<sup>[18](https://learn.microsoft.com/dotnet/standard/clr)</sup>。CLR 处理内存分配和管理，是一个使用 JIT 编译器即时生成和编译代码的虚拟机<sup>[19](https://learn.microsoft.com/dotnet/standard/glossary)</sup>。

## 执行策略：解释、即时编译与提前编译

运行时选择程序代码如何变为可执行的机器码。CPython 只有解释器，没有 JIT 编译器；OpenJDK 和 V8/Node.js 先解释字节码，再对热点函数进行即时编译；Go 是提前编译的<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup>。

**即时编译。** JIT 编译器在运行期将字节码翻译为目标机器的本地指令集。这一思想最早于七十年代初提出，最早的构想之一可追溯到麦卡锡（McCarthy）1960 年的 [Lisp](https://www.edgepedia.org/zh/lisp-programming-language) 论文，随着 Java 以及 [JavaScript](https://www.edgepedia.org/zh/javascript) 等动态语言的采用而重获新生<sup>[20](https://www.cs.columbia.edu/~aho/cs6998/reports/12-12-11_Ramanan_JIT.pdf)</sup>。JIT 通常隔离出运行期频繁访问的代码段——热路径——并以激进优化对其进行静态编译。热路径可以在不同粒度上识别：基于方法的识别在 Java 等静态类型语言的 JIT 中很常见，较为简单，但会编译异常处理器等非热点段；而基于跟踪的 JIT 编译选择更细粒度的跟踪。由于同时运行编译器和解释器代价高昂，且在解释代码和编译代码之间不断切换会导致执行不流畅，JIT 系统使用翻译缓存等数据结构来快速引用编译后的代码<sup>[20](https://www.cs.columbia.edu/~aho/cs6998/reports/12-12-11_Ramanan_JIT.pdf)</sup>。当虚拟机注意到某段代码被频繁执行时，它会调用 JIT 编译器以更高的优化级别重新编译该代码，这一机制最早在 Self 编程语言中设计；栈上替换（OSR）用于 [Jikes RVM](https://www.edgepedia.org/zh/jikes-rvm)、HotSpot JVM 和 V8 JavaScript 引擎等现代虚拟机<sup>[21](https://openresearch-repository.anu.edu.au/bitstreams/82882d11-2bbb-401e-94d4-96afb3b6d88b/download)</sup>。

**提前编译。** AOT 编译在应用程序执行之前将中间语言翻译为机器码，通常在另一台机器上进行。由于 AOT 工具链不在运行期编译，它们可以花更多时间进行优化；由于上下文是整个应用程序，AOT 编译器还执行跨模块链接和全程序分析，生成单个可执行文件。.NET 的 CoreRT 与 CLR 不同，它不是虚拟机：它包含垃圾回收器和运行时类型识别，但没有即时生成和运行代码的设施<sup>[19](https://learn.microsoft.com/dotnet/standard/glossary)</sup>。

## 性能特征

定量比较显示了执行策略和运行时设计的重要性。在一项将四种托管语言与 GCC 上的 C++ 进行对比的研究中，Go 和 OpenJDK 接近原生基线，平均运行耗时分别为基线的 1.30 倍和 1.43 倍，并且在 12 个基准测试中各有 3 个优于 GCC；V8/Node.js 和 CPython 的平均运行耗时分别为基线的 8.01 倍和 29.50 倍，其中 CPython 在排序基准测试中的耗时高达基线的 129.66 倍。只有当工作负载受磁盘 I/O 瓶颈限制时，V8/Node.js 和 CPython 才能与 GCC 竞争<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup>。

**启动与并发。** 解释器性能曾被轻视，认为只影响分摊后的启动时间，如今又变得重要，因为云自动扩缩容和函数即服务平台会启动短生命周期的实例；2020 年 [AWS Lambda](https://www.edgepedia.org/zh/aws-lambda) 调用的中位运行时间仅为 60 毫秒，而 JVM 和 V8 的启动时间在数百毫秒或数秒量级<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup>。并发模型也因运行时而异：V8/Node.js 是事件驱动的，处理程序在单线程上顺序执行；CPython 的 GIL 只允许同一时刻有一个线程执行；Go 的运行时将称为 goroutine 的用户线程调度到内核线程上；OpenJDK 的线程就是内核线程。在该研究中，增加线程实际上减慢了并行的 V8 和 CPython 应用程序，因为需要更多的串行化，而 Go 在多线程键值存储基准测试中比 GCC 实现了 1.02 倍的加速<sup>[7](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)</sup>。

## 现代运行时的安全性

由于运行时代表程序执行代码，它也是一个强制执行点。JITDomain 研究项目针对利用 JIT 编译代码中的内存漏洞的攻击，这类攻击可以转移执行流，并直接或通过面向返回编程（Return-Oriented Programming）链接机器指令来构造载荷；JITDomain 复制内存访问指令并将其绑定到特定域，以对 JIT 代码实施三项保证：调用栈隔离、数据访问分离和系统调用过滤<sup>[22](https://doi.org/10.1016/j.micpro.2026.105285)</sup>。

[WebAssembly](https://www.edgepedia.org/zh/webassembly) 运行时将沙箱化作为核心设计目标。[Wasmtime](https://www.edgepedia.org/zh/wasmtime) 是构建在 Cranelift 代码生成器上的 WebAssembly、WASI 和组件模型的独立运行时，经过沙箱化处理，Wasm 程序无法访问任何未明确授予的资源，其开发者依赖广泛的模糊测试和形式化验证，因为 Wasm 的理论安全性再强，也受限于运行时实际实现的安全性<sup>[8](https://bytecodealliance.org/articles/wasmtime-portability)</sup><sup> • </sup><sup>[23](https://docs.wasmtime.dev/introduction.html)</sup>。默认情况下，Wasmtime 拒绝组件访问所有系统资源，包括文件系统和环境变量<sup>[24](https://component-model.bytecodealliance.org/running-components/wasmtime.html)</sup>。研究系统在此基础上构建：Wasabi 无服务器系统在同一容器沙箱内安全地共置来自不同客户的 Wasm 应用程序，使用 WebAssembly 等语言运行时来降低隔离开销而不牺牲安全性<sup>[25](https://www.usenix.org/conference/nsdi26/presentation/baqershahi)</sup>。

## 简史

运行时服务早于现代虚拟机。垃圾回收由 Lisp 于 1960 年左右引入，Lisp 系统后来被系统地研究其性能权衡；斯坦福 Lisp 性能研究描述了主要 Lisp 方言的实现技术和基准测试<sup>[26](https://ocw.mit.edu/courses/6-001-structure-and-interpretation-of-computer-programs-spring-2005/dfd6825821a8081af7e65f7c132ef7f3_lecture2lispstor.pdf)</sup><sup> • </sup><sup>[27](https://mitpress.mit.edu/9780262571937/performance-and-evaluation-of-lisp-systems/)</sup>。后来编码在 Java HotSpot 虚拟机中的许多思想源于早期的 [Smalltalk](https://www.edgepedia.org/zh/smalltalk) 和 Self 实现，这些实现将单个方法动态编译为本地机器码，以加速原本缓慢的 Smalltalk 字节码解释器<sup>[28](https://theses.gla.ac.uk/74957/1/11007878.pdf)</sup>。研究基础设施也沿着同样的路径发展：Joeq 是一个作为开源发布的虚拟机和编译器基础设施，旨在促进对 JIT 和提前编译、高级垃圾回收、分布式计算和高级运行时技术的研究，并且与语言无关，因此任何受支持语言的代码都可以被动态编译、链接和执行<sup>[29](https://www.sciencedirect.com/science/article/pii/S0167642305000390)</sup>。

## References

1. [Chappell, CS 331 Compilers & Interpreters course notes, University of Alaska Fairbanks](https://www.cs.uaf.edu/~chappell/class/2026_spr/cs331/read/comp_int.html)
2. [Runtime Systems and Memory Management, LearnlyAI](https://learnlyai.co.uk/en/course-hub/computer-science/programming-languages-paradigms/compilers-and-interpreters/runtime-systems-and-memory-management)
3. [Runtime system (court reference document)](https://www.lb7.uscourts.gov/documents/13c78953.pdf)
4. [Programming language, TechPedia](https://techpedia.wiki/en/wiki/Programming_language)
5. [Clarkson, Compiler and Runtime Support, PhD thesis, University of Manchester](https://apt.cs.manchester.ac.uk/ftp/pub/apt/theses/Clarkson18_phd.pdf)
6. [Hybrid Runtime Systems technical report, Northwestern University](https://www.mccormick.northwestern.edu/computer-science/documents/tech-reports/2016/2016-12-hybrid-runtime-systems.pdf)
7. [Investigating Managed Language Runtime Performance, USENIX ;login:](https://www.usenix.org/publications/loginonline/investigating-managed-language-runtime-performance)
8. [Making WebAssembly and Wasmtime More Portable, Bytecode Alliance](https://bytecodealliance.org/articles/wasmtime-portability)
9. [Runtime Environments course notes, CS3501](https://www.shreesathyam.edu.in/files/CM/CSE/5TH/CS3501/CS3501-NOTES.pdf)
10. [The Foundations and Evolution of Runtime Systems, BitMinds](https://www.bitminds.org/the-foundations-and-evolution-of-runtime-systems/)
11. [Run-Time Dynamic Linking, Win32 apps, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/run-time-dynamic-linking)
12. [The Java Virtual Machine Specification, Java SE 26 Edition, Chapter 2](https://docs.oracle.com/javase/specs/jvms/se26/html/jvms-2.html)
13. [CS 17-670: Advanced Topics in WebAssembly and Managed Runtime Systems, Carnegie Mellon University](https://www.cs.cmu.edu/~wasm/cs17-670/fall2022/)
14. [Programming Languages lecture notes, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=05db5fc48b7d4aef0ae397f21dd7b52ae18d63ef&repid=rep1&type=pdf)
15. [The Java Virtual Machine Specification, Java SE 21 Edition, Chapter 2](https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html)
16. [Sun Global Glossary: runtime system](https://docs.oracle.com/cd/E19957-01/805-4368/6j450e60s/index.html)
17. [JEP 444: Virtual Threads, OpenJDK](https://openjdk.org/jeps/444)
18. [Common Language Runtime (CLR) overview, Microsoft Learn](https://learn.microsoft.com/dotnet/standard/clr)
19. [.NET glossary, Microsoft Learn](https://learn.microsoft.com/dotnet/standard/glossary)
20. [Ramanan, JIT through the ages, Columbia University](https://www.cs.columbia.edu/~aho/cs6998/reports/12-12-11_Ramanan_JIT.pdf)
21. [Managed Runtime Environments, §2.4, ANU open research repository](https://openresearch-repository.anu.edu.au/bitstreams/82882d11-2bbb-401e-94d4-96afb3b6d88b/download)
22. [Ducasse, Cotret, Lagadec, JITDomain: Instruction-level JIT code isolation, Microprocessors and Microsystems](https://doi.org/10.1016/j.micpro.2026.105285)
23. [Introduction, Wasmtime documentation](https://docs.wasmtime.dev/introduction.html)
24. [Wasmtime and the WebAssembly Component Model, Bytecode Alliance](https://component-model.bytecodealliance.org/running-components/wasmtime.html)
25. [Baqershahi et al., Hierarchical Integration of WebAssembly in Serverless, NSDI 26](https://www.usenix.org/conference/nsdi26/presentation/baqershahi)
26. [Graham, Using Lisp in Startup, MIT OCW 6.001 lecture notes](https://ocw.mit.edu/courses/6-001-structure-and-interpretation-of-computer-programs-spring-2005/dfd6825821a8081af7e65f7c132ef7f3_lecture2lispstor.pdf)
27. [Gabriel, Performance and Evaluation of LISP Systems, MIT Press](https://mitpress.mit.edu/9780262571937/performance-and-evaluation-of-lisp-systems/)
28. [Runtime Evolution of Java Applications, PhD thesis, University of Glasgow](https://theses.gla.ac.uk/74957/1/11007878.pdf)
29. [Joeq: A virtual machine and compiler infrastructure, Science of Computer Programming](https://www.sciencedirect.com/science/article/pii/S0167642305000390)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/runtime-system

English original: https://www.edgepedia.org/runtime-system
