{"id":16867907,"url":"https://github.com/fornwall/allocation-counter","last_synced_at":"2025-03-22T07:30:51.954Z","repository":{"id":57484616,"uuid":"319120780","full_name":"fornwall/allocation-counter","owner":"fornwall","description":"Rust library to measure memory allocations of a function or closure.","archived":false,"fork":false,"pushed_at":"2023-09-21T10:25:51.000Z","size":40,"stargazers_count":18,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T09:12:51.083Z","etag":null,"topics":["memory-allocations","memory-management","performance","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fornwall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-06T20:05:13.000Z","updated_at":"2024-12-12T22:14:12.000Z","dependencies_parsed_at":"2024-10-28T12:47:04.726Z","dependency_job_id":null,"html_url":"https://github.com/fornwall/allocation-counter","commit_stats":{"total_commits":11,"total_committers":1,"mean_commits":11.0,"dds":0.0,"last_synced_commit":"48dfd721546c6024bd63b8a3424b88616d697472"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fornwall%2Fallocation-counter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fornwall%2Fallocation-counter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fornwall%2Fallocation-counter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fornwall%2Fallocation-counter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fornwall","download_url":"https://codeload.github.com/fornwall/allocation-counter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244924943,"owners_count":20532872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["memory-allocations","memory-management","performance","rust"],"created_at":"2024-10-13T14:56:05.093Z","updated_at":"2025-03-22T07:30:49.257Z","avatar_url":"https://github.com/fornwall.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Crates.io](https://img.shields.io/crates/v/allocation-counter.svg)](https://crates.io/crates/allocation-counter)\n[![Docs](https://docs.rs/allocation-counter/badge.svg)](https://docs.rs/allocation-counter/)\n[![Build](https://github.com/fornwall/allocation-counter/workflows/CI/badge.svg)](https://github.com/fornwall/allocation-counter/actions?query=workflow%3A%22CI%22)\n\n# allocation-counter\nRust library to run code while counting allocations. Can be used to explore memory allocation usage, or assert that the desired amount of memory allocations is not exceeded in tests.\n\nIt works by replacing the System allocator with a custom one which increases a thread local counter on each memory allocation before delegating to the normal system allocator.\n\nSee the below example and the [crate documentation](https://docs.rs/allocation-counter/latest/allocation_counter/) for more information.\n\n# Example\nAdd as a dependency - since including the trait replaces the global memory allocator, you most likely want it gated behind a feature:\n\n```toml\n[features]\ncount-allocations = [\"allocation-counter\"]\n\n[dependencies]\nallocation-counter = { version = \"0\", optional = true }\n```\n\nTests can now be written to assert that the number of desired memory allocations are not exceeded:\n\n```rust\n#[cfg(feature = \"count-allocations\")]\n#[test]\npub fn no_memory_allocations() {\n    // Verify that no memory allocations are made:\n    let info = allocation_counter::measure(|| {\n        code_that_should_not_allocate();\n    });\n    assert_eq!(info.count_total, 0);\n\n    // Let's use a case where some allocations are expected.\n    let info = allocation_counter::measure(|| {\n        code_that_should_allocate_a_little();\n    });\n\n    // Using a lower bound can help track behaviour over time:\n    assert!((500..600).contains(\u0026info.count_total));\n    assert!((10_000..20_000).contains(\u0026info.bytes_total));\n\n    // Limit peak memory usage:\n    assert!((100..200).contains(\u0026info.count_max));\n    assert!((1_000..2_000).contains(\u0026info.bytes_max));\n\n    // We don't want any leaks:\n    assert_eq!(0, info.count_current);\n    assert_eq!(0, info.bytes_current);\n\n    // It's possible to opt out of counting allocations\n    // for certain parts of the code flow:\n    let info = allocation_counter::measure(|| {\n        code_that_should_not_allocate();\n        allocation_counter::opt_out(|| {\n            external_code_that_should_not_be_tested();\n        });\n        code_that_should_not_allocate();\n    });\n    assert_eq!(0, info.count_total);\n}\n```\n\nRun the tests with the necessary feature enabled:\n\n```sh\ncargo test --features count-allocations\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffornwall%2Fallocation-counter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffornwall%2Fallocation-counter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffornwall%2Fallocation-counter/lists"}