diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index 5a3a73bd3..73f96e87c 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1482,6 +1482,15 @@ impl Regex { pub fn locations(&self) -> CaptureLocations { self.capture_locations() } + + /// Return the total approximate heap memory, in bytes, used by this `Regex`. + /// + /// Note that this value does not have a defined relationship to + /// to [`RegexBuilder::size_limit`]. + #[inline] + pub fn memory_usage(&self) -> usize { + self.meta.memory_usage() + self.pattern.len() + } } /// Represents a single match of a regex in a haystack. diff --git a/src/regex/string.rs b/src/regex/string.rs index 627a6c137..738fa1a71 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1481,6 +1481,15 @@ impl Regex { pub fn locations(&self) -> CaptureLocations { self.capture_locations() } + + /// Return the total approximate heap memory, in bytes, used by this `Regex`. + /// + /// Note that this value does not have a defined relationship to + /// to [`RegexBuilder::size_limit`]. + #[inline] + pub fn memory_usage(&self) -> usize { + self.meta.memory_usage() + self.pattern.len() + } } /// Represents a single match of a regex in a haystack.