• [翻译]编写高性能 .NET 代码 第二章:垃圾回收


    返回目录

    第二章:垃圾回收

    垃圾回收是你开发工作中要了解的最重要的事情。它是造成性能问题里最显著的原因,但只要你保持持续的关注(代码审查,监控数据)就可以很快修复这些问题。我这里说的“显著的原因”,实际上是我们对垃圾回收的理解和期望不正确导致的。在.NET开发中,内存的性能问题和CPU的性能问题一样多,这就是单独开一章主要描述这个问题的原因。

    当我们提及垃圾回收造成的开销时,就会不如自主的紧张起来,但一旦你理解它,就能很好的优化你的程序。在后面文章里,你可以看到GC可以在大多数情况下,在堆处理上提供很好的性能,同时也能很好解决内存分配与内存碎片问题。

    Windows在非托管堆里用使用一个空闲列表来维护内存分配。尽管它会想尽办法来减少内存碎片,但很多长时间运行的非托管代码(本地代码)的程序还是会碰上内存碎片问题。它会花很多时间在空闲列表里找到合适可分配地址。随着内存使用持续增长,不可避免的需要不断重启来解问题。一些程序还会采用自定义内存分配的方案(自己的内存分配算法来接管malloc)函数来解决内存碎片问题。

    .NET里的内存分配通常在一个内存段里进行,这样申请和回收的消耗会小很多。托管堆通过将最近申请的内存对象放在一起,可以减少对空闲列表的遍历,提升性能。

    在默认的分配过程中,通过代码获得对象的大小,然后在剩余的缓冲区里分配它。因为没有竞争,只要有合适的空间就能很快的分配。一旦这要申请的空间这一段无法满足,GC分配器会创建一个新的内存段,在这上面开始分配,之后的新的分配也都会在这新创建的内存段里进行。

    这个过程中系统代码(分配器)只会做一些简单的检查。

    我们来看一个简单的栗子:

    private class MyObject
    {
        private int x;
        private int y;
        private int z;
    }
    
    private static void Main(string[] args)
    {
        var x = new MyObject();
    }
    
    

    首先,我们在分配器前设置一个断点

    ; Copy method table pointer for the class into
    ; ecx as argument to new()
    ; You can use !dumpmt to examine this value.
    mov ecx,3F3838h
    
    ; Call new
    call 003e2100
    
    ; Copy return value (address of object) into a register
    mov edi,eax
    
    

    目前分配的是:

    ; NOTE: Most code addresses removed for formatting reasons.
    ;
    ; Set eax to value 0x14, the size of the object to
    ; allocate, which comes from the method table
    mov eax,dword ptr [ecx+4] ds:002b:003f383c=00000014
    
    ; Put allocation buffer information into edx
    mov edx,dword ptr fs:[0E30h]
    
    ; edx+40 contains the address of the next available byte
    ; for allocation. Add that value to the desired size.
    add eax,dword ptr [edx+40h]
    
    ; Compare the intended allocation against the
    ; end of the allocation buffer.
    cmp eax,dword ptr [edx+44h]
    
    ; If we spill over the allocation buffer,
    ; jump to the slow path
    ja 003e211b
    
    ; update the pointer to the next free
    ; byte (0x14 bytes past old value)
    mov dword ptr [edx+40h],eax
    
    ; Subtract the object size from the pointer to
    ; get to the start of the new obj
    sub eax,dword ptr [ecx+4]
    
    ; Put the method table pointer into the
    ; first 4 bytes of the object.
    ; eax now points to new object
    mov dword ptr [eax],ecx
    
    ; Return to caller
    ret
    
    ; Slow Path – call into CLR method
    003e211b jmp clr!JIT_New (71763534)
    
    

    总之,这些涉及到方法调用的指令只有9个,不是很难懂。
    如果你使用了一些配置选项,例如工作站模式,它不会因为竞争而导致分配变慢,因为GC给每一个处理器(cpu内核)分配了一个堆(段)。.NET在这些内存分配地方做了一些复杂处理,但你不必深入了解它是如何工作,只需要知道如何优化它就可以了。

    我在本书开头就涉及到垃圾回收是因为今后章节里很多东西都会涉及到它。正确的理解垃圾回收是帮助你实现好性能的基础。

    下一节 >> 基本操作

  • 相关阅读:
    模糊查询(排除%等通配符并支持不连续关键字查询)
    ideal中运行manven常用操作
    ideal项目启动及问题
    FastJSON 转换List<T> ,Map<T,T>泛型失败 处理方法
    MySQL的SELECT ...for update
    CouchDB客户端开发—Java版
    Spring Data JPA 实例查询
    第一章 计算机网络概述
    第二章 物理层(二)
    Java常考面试题(一)
  • 原文地址:https://www.cnblogs.com/yahle/p/6550044.html
Copyright © 2020-2023  润新知