惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Scott Helme
Scott Helme
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
Project Zero
Project Zero
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
S
Schneier on Security
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
J
Java Code Geeks
IT之家
IT之家
爱范儿
爱范儿
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Jina AI
Jina AI
腾讯CDC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
T
Threatpost
V
V2EX
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
美团技术团队
小众软件
小众软件
NISL@THU
NISL@THU
Simon Willison's Weblog
Simon Willison's Weblog
T
The Exploit Database - CXSecurity.com
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
V
Vulnerabilities – Threatpost
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
A
Arctic Wolf
U
Unit 42
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

Datadog | The Monitor blog

Introducing our open source AI-native SAST Instrument and monitor Boomi integration flows with OpenTelemetry and Datadog Not all index scans are equal: How we cut query latency by over 99% Platform engineering metrics: What to measure and what to ignore Integrate Recorded Future threat intelligence with Datadog Cloud SIEM CI/CD security: threat modeling using a MITRE-style threat matrix CI/CD security: How to secure your GitHub ecosystem Ingress NGINX is EOL: A practical guide for migrating to Kubernetes Gateway API Operating agentic AI with Amazon Bedrock AgentCore and Datadog LLM Observability: Lessons from NTT DATA Introducing the Datadog Code Security MCP Capture and analyze custom heatmaps in Session Replay Understand session replays faster with AI summaries and smart chapters Monitor ClickHouse query performance with Datadog Database Monitoring How we designed empathetic alert sounds for on-call engineers Search and act across Datadog to resolve issues faster with Bits Assistant Measure the business impact of every product change with Datadog Experiments Analyzing round trip query latency Configuring JavaScript caches for better performance Introducing Bits AI Dev Agent for Code Security Datadog achieves ISO 42001 certification for responsible AI Monitor Nutanix clusters, hosts, and VMs with Datadog Monitor Juniper Mist in Datadog A new Host Map for modern infrastructure Annotate traces to improve LLM quality with Datadog LLM Observability What’s new in Cloud SIEM: AI-powered investigations, enhanced threat intelligence, and scalable security operations Explore Kubernetes with native OpenTelemetry data Monitor Oracle Fusion Cloud Applications with Datadog Announcing the Datadog Terraform provider v4.0.0 Scaling Kubernetes workloads on custom metrics How to design cloud environments for AI-powered threat analysis Monitor Aruba Central in Datadog How we centralize and remediate risks with Datadog Case Management Accelerate incident response with Datadog and ServiceNow Monitor your application and network load balancer logs Understanding Karpenter architecture for Kubernetes autoscaling Tools for collecting metrics and logs from Karpenter Monitor Karpenter with Datadog What your product data is actually saying Key metrics for monitoring Karpenter Securing Datadog’s platform in the AI age: The role of observability data Four ways engineering teams use the Datadog MCP Server to power AI agents Approaching your observability migration with the right mindset Meet the new Bits AI SRE: Deeper reasoning, twice as fast Key learnings from the 2026 State of DevSecOps study Use plain English to query your multi-cloud infrastructure in Resource Catalog Simplifying troubleshooting across the user journey with Datadog Synthetic Monitoring Protect your OCI resources with Datadog Cloud Security This Month in Datadog - February 2026 Amazon EC2 security: How misconfigured and public AMIs expand your cloud attack surface Enable end-to-end visibility into your Java apps with a single command Measure and improve mobile app startup performance with Datadog RUM Evaluating our AI Guard application to improve quality and control cost Identify untested code across every level of your codebase Make use of guardrail metrics and stop babysitting your releases Monitor Versa Networks SD-WAN performance in Datadog Improve performance and reliability with APM Recommendations Remediate transitive vulnerabilities faster with Datadog Software Composition Analysis Generate audit-ready vulnerability and compliance reports with Datadog Sheets Monitor Fortinet FortiManager performance in Datadog Improve test coverage across codebases with Datadog Code Coverage Move fast, don’t break things: Consistent testing standards at scale Enrich logs with ServiceNow CMDB context before routing to any SIEM or logging tool Monitor Lustre with Datadog Make faster, better product decisions with Datadog Product Analytics Surface and remediate runtime posture issues with Workload Protection Findings Protect agentic AI applications with Datadog AI Guard How to optimize JavaScript code with CSS Trace Google Pub/Sub workloads in Cloud Run with Datadog Detect human names in logs with ML in Sensitive Data Scanner How we cut our NLQ agent debugging time from hours to minutes with LLM Observability Debug PostgreSQL query latency faster with EXPLAIN ANALYZE in Datadog Database Monitoring Datadog acquires Propolis Unify and correlate frontend and backend data with retention filters Scale compliance across global frameworks with Datadog Cloud Security Monitor Arista VeloCloud SD-WAN performance with Datadog Building reliable dashboard agents with Datadog LLM Observability Simplify log collection and aggregation for MSSPs with Datadog Observability Pipelines Mitigation for Node.js denial-of-service vulnerability affecting Datadog APM Automate flaky test fixes with the Bits AI Dev Agent and Test Optimization How we built an AI SRE agent that investigates like a team of engineers Datadog integrations 2025 recap: Observability for AI, security, and hybrid cloud Design effective executive dashboards with Datadog Implement dbt data quality checks with dbt-expectations Bring faster visibility into AWS Lambda functions with remote instrumentation Troubleshoot faster with the GitLab Source Code integration in Datadog How Cambia Health Solutions saved $30,000 monthly with Cloud Cost Management and the Datadog Resource Catalog Normalize any logs for Cloud SIEM with Datadog's OCSF processor Optimizing Datadog at scale: Cost-efficient observability at Zendesk Detect, diagnose, and resolve network issues easily with CNM Network Health Connect engineering errors to user impact in early-stage products Cilium configuration for Kubernetes operations at scale Designing feedback loops for progressive delivery Ship features faster and safer with Datadog Feature Flags Choosing the right OpenTelemetry Collector distribution Route your monitor alerts with Datadog monitor notification rules Automate Cloud SIEM investigations with Bits AI Security Analyst Cloud threat detection: How to identify risky activity across control and data planes Collecting Kafka performance metrics Monitoring Kafka with Datadog Monitoring Kafka performance metrics
Python memory profiling: Common pitfalls and how to avoid them
2025-12-18 · via Datadog | The Monitor blog
Bowen Chen

Bowen Chen

Nenad Noveljić

Nenad Noveljić

Continuous profiling has established itself as core observability practice, so much so that we’ve referred to it as the fourth pillar of observability. But despite the capabilities and growing adoption of continuous profiling, it can still be confusing to approach profiling as a newcomer and correctly apply it to different troubleshooting scenarios. For example, when using a flame graph to correlate memory issues to specific lines of code, you might see a wide span and think, “This function is hoarding memory,” which isn’t always true.

In this blog post, we’ll discuss a common misconception surrounding memory allocation and memory retention, and also when to visualize your flame graph using heap live size versus allocated memory. We’ve provided demo Python code for both topics, so you can try and recreate a demo example in your local environment to help further your understanding.

Memory allocation versus memory retention

You likely have some sort of alerting or dashboard configured to monitor your application’s memory usage. For Python applications, this may be memory usage grouped by container or monitoring the RSS of different processes. When these metrics steadily grow over time, it often indicates that objects are being held longer than intended, in which case you’ll need to investigate the data structures that are holding memory.

To illustrate this example, we have the following Python file allocator_vs_holder.py. In it, we create a list (cache) that holds several objects (obj) that are each allocated a large portion of memory. While this is a simplified representation, it reflects what you’d typically investigate when faced with an increased memory footprint: i.e., trying to surface the objects that are holding references (in this case, the bytearray obj instantiated in line 12 of our code).

import time

from ddtrace.profiling import Profiler

cache = [] # This is the "holder" – it keeps references alive.

def allocate_then_return(n_bytes: int) -> bytearray:

# "Allocator": creates the memory and returns it.

return bytearray(n_bytes)

def make_and_hold(items: int, size: int) -> None:

for _ in range(items):

obj = allocate_then_return(size)

# The *holder* is here: we keep references so objects stay live.

cache.append(obj)

time.sleep(0.05)

if __name__ == "__main__":

Profiler().start()

# Hold a few big objects (live heap goes up)

make_and_hold(items=6, size=5_000_000) # ~30MB retained

# Keep process alive for a couple profile collections

time.sleep(90)

A common use case for the Datadog Continuous Profiler is to help troubleshoot these memory issues, and many developers may instinctively default to using the heap live size view to get a sense of the processes currently occupying memory in use. However, the common pitfall when using the heap live size view is assuming that the spans direct you to the retaining path when in reality, the spans point to where the memory is being allocated and not held.

The heap live size view helps identify memory allocation, not retention.

Consider the heap live size flame graph for the previous example. Typically, you might defer to the top of the call stack and be inclined to believe that this is the function holding memory. However, knowing our code, what we’re actually interested in is not the allocate_then_return function at the top of our call stack but rather line 12 of the make_and_hold function where obj is keeping our references live.

One method that can lead you to the memory retaining code is to investigate the retainer objects themselves. By adding debugging code to our main block, we can attempt to programmatically extract more information:

# Retained memory by type

from pympler import muppy, asizeof

all_objs = muppy.get_objects()

items = []

for o in all_objs:

try:

items.append((asizeof.asizeof(o), type(o).__name__, repr(o)[:120]))

except Exception:

pass

for size, typ, rep in sorted(items, reverse=True)[:20]:

print(f"{size/1024/1024:8.1f} MB {typ:20} {rep}")

In this debugging code, we’re using muppy.get_objects, which is a function from Python’s Pympler library that returns a list of all currently tracked Python objects. Using our print function, we can sort the largest objects currently held in memory and try to gather clues regarding their size, type, and content. Using the output, we can determine that the memory holder is a list of bytearrays, which enables us to tie it tocache. Python garbage-collects an object when there are no longer any references to it, but since cache is a global variable, it holds persistent reference to each item in its list. Therefore, we can conclude that the memory retaining code in this example is line 14, where cache appends each bytearray and establishes the reference.

50.6 MB module <module '__main__' from '/Users/nenad.noveljic/Python/allocator_vs_holder.py'>

28.6 MB list [bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0

4.8 MB bytearray bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

4.8 MB bytearray bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

4.8 MB bytearray bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

4.8 MB bytearray bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

4.8 MB bytearray bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

4.8 MB bytearray bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

When to use heap live size vs. allocated memory

At this point, you’re probably wondering, “If the heap live size view is showing me where allocations occur, how does it differ from my profiler’s allocated memory view, and how do I determine which view to use?” In short, the heap live size only displays allocated memory for functions that are still in use, while the allocated memory view helps you visualize memory allocations regardless of whether the objects remain in use or if they were subsequently freed by garbage collection. Determining when to use which of these views is typically based on the nature of the issue you’re investigating.

To provide an illustrative example, consider the following code in which we have two functions: grow_heap, which adds large byte arrays to a persistent list, and heavy_churn, which allocates memory and frees it almost instantly.

import time

from ddtrace.profiling import Profiler

bag = []

def grow_heap():

# Grow to a stable plateau by retaining big objects

for _ in range(8):

cache.append(bytearray(4_000_000)) # ~32MB total

time.sleep(0.05)

def heavy_churn():

# High allocation rate; most objects die quickly

for _ in range(300_000):

_ = bytearray(1024)

if __name__ == "__main__":

Profiler().start()

grow_heap() # increases live heap

heavy_churn() # spikes allocations without growing heap much

time.sleep(90)

grow_heap is very similar to our previously discussed example, in that the code creates a growing, persistent memory footprint. For this issue, we recommend using the heap live size view, because you want to find out what functions are using live memory (and remember to investigate the memory holder and not allocator). Looking at the corresponding code profile, the heap live size view directs us to line 9 in our grow_heap function, which is where the large byte arrays are allocated and then added to the list.

Use the heap live size view to troubleshoot growing memory footprints.

Similarly, the heap live size also lets us troubleshoot increases in memory footprint following events such as new deployments, traffic spikes, or changes to infrastructure. The Datadog Continuous Profiler’s compare feature enables you to perform a side-by-side comparison of memory profiles from different time periods and visualize how the memory footprint of different functions have changed over time.

Not all memory issues revolve around growing memory footprints. Often, your hosts and applications can suffer from high CPU usage and increased garbage collection latency as a result of heavy churn (when objects are frequently allocated and freed). In these cases, we’re less concerned with the live objects in our heap and more interested in the code that is creating objects. Using the profiler’s allocated memory view, we can visualize the spans that are allocating the most memory per minute. In this case, the top of our call stack directs us to line 20 in our heavy_churn function, which is where byte arrays are continuously allocated and freed.

Use the allocated memory view to investigate

Get Started with Datadog today

Profiling memory issues isn’t always as simple as investigating the span at the top of the stack, or even following the call stack as we did in this example. Often, the memory retaining path does not overlap with the call path that allocates memory, and surfacing it requires a deep understanding of the source code you’re investigating. To help simplify your investigation, Datadog is currently developing native solutions to identify memory retaining code paths for Java and .NET.

To start profiling your Python applications, check out our documentation for the Datadog Continuous Profiler, or explore our other blogs and guides on code profiling. If you’re new to Datadog, sign up for a 14-day free trial.