Pro .NET Memory Management Companion Code

repository·master·Indexed 41 days ago

https://github.com/apress/pro-.net-memory

Companion source code and assets for the book Pro .NET Memory Management by Konrad Kokosa. Includes CoreCLR.ZeroGC, a minimal Garbage Collector implementation for CoreCLR, as well as C++ benchmarks using the hayai framework and various memory leak demonstration libraries.

Tokens
1.5K
Snippets
4
Records
10
Agent score
51%

What's inside Pro .NET Memory Management

  1. Overview of the Unmanaged Library project structure

    master

    The UnmanagedLibrary project is a Dynamic Link Library (DLL) generated using the Visual C++ Application Wizard. It consists of the following key components:

    • Main DLL Source File: The primary source file containing the implementation logic for the DLL.
    • Project File: Contains configuration for Visual C++ versions, platforms, and project features.
    • Filters File: Manages file grouping within the IDE (e.g., associating .cpp files with 'Source Files').
    • Precompiled Headers (StdAfx.h, StdAfx.cpp): Used to build the .pch (precompiled header) and StdAfx.obj files to improve build performance.

    Note on Customization: The source code contains TODO: comments to indicate specific areas where you should add or customize implementation logic.

  2. Overview of CoreCLR.ZeroGC

    master

    CoreCLR.ZeroGC is a minimal Garbage Collector (GC) implementation for CoreCLR. Unlike the default GC, it does not automatically reclaim memory. It provides the bare minimum functionality required by the Execution Engine to allow object allocation without the overhead of automatic memory management.

    Use Cases

    • Custom GC Development: Serves as a foundational implementation for developers building their own custom garbage collection mechanisms.
    • Zero-Allocation/No-Alloc Programming: Useful for specialized applications with extremely short lifespans or those designed to perform almost no memory allocations, where the overhead of a standard GC is unnecessary.
  3. Project structure of the CppBenchmark application

    master

    The CppBenchmark application is a VC++ console application generated by AppWizard. The project structure consists of the following key components:

    • CppBenchmark.vcxproj: The main Visual C++ project file containing platform, configuration, and feature settings.
    • CppBenchmark.vcxproj.filters: An IDE-specific file that manages the grouping of files (e.g., associating .cpp files with the 'Source Files' node).
    • CppBenchmark.cpp: The primary source file containing the application's entry point and main logic.
    • StdAfx.h & StdAfx.cpp: Files used to implement Precompiled Headers (PCH) to improve build times, resulting in CppBenchmark.pch and StdAfx.obj.

    Note for developers: Look for TODO: comments within the source code to identify areas intended for customization or additional implementation.

  4. Project Overview: MemoryLeaks.Library DLL

    master

    The MemoryLeaks.Library is a Dynamic Link Library (DLL) project generated via the AppWizard. It is designed to demonstrate memory leak scenarios within a C++/CLI or C++ context. The project consists of the following core components:

    • MemoryLeaks.Library.cpp: The primary source file containing the DLL implementation logic.
    • MemoryLeaks.Library.h: The header file containing the class declarations.
    • AssemblyInfo.cpp: Used for defining custom attributes to modify assembly metadata.
    • MemoryLeaks.Library.vcxproj: The Visual C++ project file containing platform, configuration, and versioning information.
    • MemoryLeaks.Library.vcxproj.filters: IDE-specific file used to group files (e.g., grouping .cpp files under "Source Files").

    Note: Developers should look for TODO: markers within the source code to identify areas intended for customization or additional implementation.

  5. Install and download the complete repository via Git LFS

    master

    This repository uses Git Large File Storage (LFS) to manage large files. To ensure you download the actual content instead of small placeholder files, you must install Git LFS before cloning.

    1. Install Git LFS.
    2. Clone the repository using Git or GitHub Desktop.
  6. Manually download Git LFS files

    master

    If you cannot use Git LFS, you must manually download the large files from GitHub to replace the placeholder files in your local clone:

    1. Clone or download the repository as a ZIP (this will contain only placeholder files).
    2. For every large file missing content, navigate to that specific file on GitHub in your web browser.
    3. Click the Download button to retrieve the actual file from Git Large File Storage.
  7. Define application resources in app.rc

    master

    The app.rc file is a Microsoft Visual C++ generated resource script used to embed assets like icons and header files into the MemoryLeaks.Library project.

    Key resource types defined:

    • ICON: The application icon is defined by the first icon resource or the one with the lowest ID value. In this project, it uses app.ico.
    • TEXTINCLUDE: Used to include C++ header files (like resource.h and afxres.h) into the resource script during compilation.
    // Icon
    1 ICON "app.ico"
    
    // Text Includes
    1 TEXTINCLUDE
    BEGIN
        "resource.h\0"
        "\0"
    END
    
    2 TEXTINCLUDE
    BEGIN
        "#include ""afxres.h""\r\n"
        "\0"
    END