官术网_书友最值得收藏!

Memory Management

In the previous chapter, I explained a few things with a lot of hand-waving. I was talking about memory being allocated but I never told what that actually means. Now is the time to fill in the missing pieces.

Memory management is part of practically every computing system. Multiple programs must coexist inside a limited memory space, and that can only be possible if the operating system is taking care of it. When a program needs some memory, for example, to create an object, it can ask the operating system and it will give it a slice of shared memory. When an object is not needed anymore, that memory can be returned to the loving care of the operating system.

Slicing and dicing memory straight from the operating system is a relatively slow operation. In lots of cases, a memory system also doesn't know how to return small chunks of memory. For example, if you call Windows' VirtualAlloc function to get 20 bytes of memory, it will actually reserve 4 KB (or 4,096 bytes) for you. In other words, 4,076 bytes would be wasted.

To fix these and other problems, programming languages typically implement their own internal memory management algorithms. When you request 20 bytes of memory, the request goes to that internal memory manager. It still requests memory from the operating system but then splits it internally into multiple parts.

In a hypothetical scenario, the internal memory manager would request 4,096 bytes from the operating system and give 20 bytes of that to the application. The next time the application would request some memory (30 bytes for example), the internal memory manager would get that memory from the same 4,096-byte block.

To move from hypothetical to specific, Delphi also includes such a memory manager. From Delphi 2006, this memory manager is called FastMM. It was written as an open source  memory manager by Pierre LeRiche with help from other Delphi programmers, and was later licensed by Borland. FastMM was a great improvement over the previous Delphi memory manager and, although it does not perform perfectly in the parallel programming world, it still functions very well after more than ten years.

Delphi exposes a public interface to replace the internal memory manager, so you can easily replace FastMM with a different memory manager. As we'll see later in this chapter, this can sometimes be helpful.

We will cover the following topics in this chapter:

  • What happens when strings and arrays are reallocated and how can we speed this up?
  • Which functions can an application use to allocate memory?
  • How can we use memory manager to dynamically create a record?
  • How is FastMM internally implemented and why does it matter to a programmer?
  • How can we replace FastMM with a different memory manager?
  • What can we do to improve SlowCode even more?
主站蜘蛛池模板: 南雄市| 黎城县| 天全县| 九台市| 漳浦县| 凭祥市| 汉中市| 黑水县| 株洲县| 霍山县| 鹿泉市| 遵义县| 达州市| 天水市| 宁南县| 开化县| 彰化县| 鸡泽县| 柳江县| 麟游县| 渑池县| 伊金霍洛旗| 黑河市| 湖口县| 哈巴河县| 五华县| 镇远县| 盐山县| 东至县| 上犹县| 东方市| 安西县| 安远县| 海口市| 珠海市| 县级市| 加查县| 盐边县| 沙田区| SHOW| 宜川县|