SlideShare uma empresa Scribd logo
1 de 13
MEMORY MANAGEMENT COMPARATIVE OF C/C+
+/JVM/PYTHON.
DANIEL FRANÇA
What’s memory
management?

The process of dynamically allocate memory when
you need and free when you don’t need it
anymore.
How it’s done?
Allocate memory from the heap with enough size
to store what you need.
Free it when you don’t need it.
It can be done either manually or automatic.
Different algorithms can be used (Ref
counting/mark and sweep, etc)
C
Manual memory management through pointers
To allocate memory you need to do a request with the size
of the memory block you want to allocate:
char* ptr = malloc(sizeof(char)*20); //Allocate enough size
for an array of chars size 19 + 1 byte reserved for the null
termination.
To free memory you call free
free(ptr); //Free memory of ptr
You need to be aware of the ownership of the pointer.
(who’s responsible for free the memory)
Pros:
Granular control.
You allocate what you need and free as soon as you
don’t need it.
Usually much lower memory footprint.
Cons
Dynamic allocations are slow and non-deterministic.
Memory-leaks can happen.
C++
Manual memory management/RAII/Smart pointers
Manual memory management is made through new and
delete operators.
RAII: The object is responsible for free the resources in its
deletion, you can see this approach in standard classes like
string and fstream.
Smart pointers: You’ve a class that encapsulates and
simulates a pointer, so you don’t need to handle the pointer
directly, the standard library of C++11 allows different types
of smart pointers like: unique_ptr, shared_ptr and weak_ptr.
Pros
A wide range of tools to help memory management.
You can use a high or low-level approach.
Small performance penalty.
Cons
Understand how the methods work can be complex.
Still memory-leaks prone.
JVM (Oracle hotspot)
Automatic Garbage Collection
Mark and sweep algorithm (mark what’s alive, the
remaining are marked as “free”)
No memory is returned to the OS
Generational GC
Different collections by age (young, old and permanent)
Memory is allocated in advance
Pros
You don’t need to worry about manual deallocations
Fine tuning of GC to optimize performance
"new" operator has only an increment pointer cost
Cons
Complex GC system
Stop-the-world event when running
Major GC is very slow
Hard to detect cause of memory leaks
Non deterministic behaviour
Python (CPython)
Automatic/Reference counting/Generational GC
Reference counting can’t handle reference
cycles, so here comes the garbage collector.
GC works based on a threshold of
allocations/deallocations.
You can disable GC for improve performance as
GC has a performance penalty.
Pros
Automatic memory management, you can focus in the algorithm.
You can call GC manually if you want.
Debug tools for GC (i.e: DEBUG_LEAK flags)
Cons
Doesn’t care about the memory usage (instead use threshold of
allocations/deallocations).
Doesn’t care about size of objects (instead it counts only the numbers).
Performance and space penalty.
Memory leaks are harder for identify the source.
Considerations
There’re different approaches trying to find the best memory
management model.
Sometimes you need to chose between performance or
memory footprint.
Non-deterministic GC is not suitable for RT applications.
There’re different tools to track memory leaks (i.e: Valgrind)
Automatic memory management doesn’t mean you don’t
need to think and understand it.
Dank u
Vragen?

Mais conteúdo relacionado

Semelhante a Memory management

Continguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelContinguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelKernel TLV
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorWednesday Solutions
 
Effective memory management
Effective memory managementEffective memory management
Effective memory managementYurii Kotov
 
Effective memory management
Effective memory managementEffective memory management
Effective memory managementDenis Zhuchinski
 
millions-gc-jax-2022.pptx
millions-gc-jax-2022.pptxmillions-gc-jax-2022.pptx
millions-gc-jax-2022.pptxTier1 app
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningCarol McDonald
 
this-is-garbage-talk-2022.pptx
this-is-garbage-talk-2022.pptxthis-is-garbage-talk-2022.pptx
this-is-garbage-talk-2022.pptxTier1 app
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Pankaj Suryawanshi
 
Share and Share Alike
Share and Share AlikeShare and Share Alike
Share and Share Alikeawebneck
 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVInternet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVArti Parab Academics
 
Fix Heap corruption in Android - Using valgrind
Fix Heap corruption in Android - Using valgrindFix Heap corruption in Android - Using valgrind
Fix Heap corruption in Android - Using valgrindCheng Hsien Chen
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Techizzaa
 
OpenCL Kernel Optimization Tips
OpenCL Kernel Optimization TipsOpenCL Kernel Optimization Tips
OpenCL Kernel Optimization TipsChamp Yen
 
Faster computation with matlab
Faster computation with matlabFaster computation with matlab
Faster computation with matlabMuhammad Alli
 
"Avoiding memory leaks in Android" Денис Жучинский
"Avoiding memory leaks in Android" Денис Жучинский"Avoiding memory leaks in Android" Денис Жучинский
"Avoiding memory leaks in Android" Денис ЖучинскийFwdays
 

Semelhante a Memory management (20)

Introduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimizationIntroduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimization
 
Continguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelContinguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux Kernel
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
 
millions-gc-jax-2022.pptx
millions-gc-jax-2022.pptxmillions-gc-jax-2022.pptx
millions-gc-jax-2022.pptx
 
Cache memory
Cache memoryCache memory
Cache memory
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
this-is-garbage-talk-2022.pptx
this-is-garbage-talk-2022.pptxthis-is-garbage-talk-2022.pptx
this-is-garbage-talk-2022.pptx
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
 
Programming for Problem Solving
Programming for Problem Solving Programming for Problem Solving
Programming for Problem Solving
 
Share and Share Alike
Share and Share AlikeShare and Share Alike
Share and Share Alike
 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVInternet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IV
 
Fix Heap corruption in Android - Using valgrind
Fix Heap corruption in Android - Using valgrindFix Heap corruption in Android - Using valgrind
Fix Heap corruption in Android - Using valgrind
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
 
C dynamic ppt
C dynamic pptC dynamic ppt
C dynamic ppt
 
OpenCL Kernel Optimization Tips
OpenCL Kernel Optimization TipsOpenCL Kernel Optimization Tips
OpenCL Kernel Optimization Tips
 
Techno-Fest-15nov16
Techno-Fest-15nov16Techno-Fest-15nov16
Techno-Fest-15nov16
 
Faster computation with matlab
Faster computation with matlabFaster computation with matlab
Faster computation with matlab
 
"Avoiding memory leaks in Android" Денис Жучинский
"Avoiding memory leaks in Android" Денис Жучинский"Avoiding memory leaks in Android" Денис Жучинский
"Avoiding memory leaks in Android" Денис Жучинский
 

Último

Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Último (20)

Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

Memory management

  • 1. MEMORY MANAGEMENT COMPARATIVE OF C/C+ +/JVM/PYTHON. DANIEL FRANÇA
  • 2. What’s memory management? The process of dynamically allocate memory when you need and free when you don’t need it anymore.
  • 3. How it’s done? Allocate memory from the heap with enough size to store what you need. Free it when you don’t need it. It can be done either manually or automatic. Different algorithms can be used (Ref counting/mark and sweep, etc)
  • 4. C Manual memory management through pointers To allocate memory you need to do a request with the size of the memory block you want to allocate: char* ptr = malloc(sizeof(char)*20); //Allocate enough size for an array of chars size 19 + 1 byte reserved for the null termination. To free memory you call free free(ptr); //Free memory of ptr You need to be aware of the ownership of the pointer. (who’s responsible for free the memory)
  • 5. Pros: Granular control. You allocate what you need and free as soon as you don’t need it. Usually much lower memory footprint. Cons Dynamic allocations are slow and non-deterministic. Memory-leaks can happen.
  • 6. C++ Manual memory management/RAII/Smart pointers Manual memory management is made through new and delete operators. RAII: The object is responsible for free the resources in its deletion, you can see this approach in standard classes like string and fstream. Smart pointers: You’ve a class that encapsulates and simulates a pointer, so you don’t need to handle the pointer directly, the standard library of C++11 allows different types of smart pointers like: unique_ptr, shared_ptr and weak_ptr.
  • 7. Pros A wide range of tools to help memory management. You can use a high or low-level approach. Small performance penalty. Cons Understand how the methods work can be complex. Still memory-leaks prone.
  • 8. JVM (Oracle hotspot) Automatic Garbage Collection Mark and sweep algorithm (mark what’s alive, the remaining are marked as “free”) No memory is returned to the OS Generational GC Different collections by age (young, old and permanent) Memory is allocated in advance
  • 9. Pros You don’t need to worry about manual deallocations Fine tuning of GC to optimize performance "new" operator has only an increment pointer cost Cons Complex GC system Stop-the-world event when running Major GC is very slow Hard to detect cause of memory leaks Non deterministic behaviour
  • 10. Python (CPython) Automatic/Reference counting/Generational GC Reference counting can’t handle reference cycles, so here comes the garbage collector. GC works based on a threshold of allocations/deallocations. You can disable GC for improve performance as GC has a performance penalty.
  • 11. Pros Automatic memory management, you can focus in the algorithm. You can call GC manually if you want. Debug tools for GC (i.e: DEBUG_LEAK flags) Cons Doesn’t care about the memory usage (instead use threshold of allocations/deallocations). Doesn’t care about size of objects (instead it counts only the numbers). Performance and space penalty. Memory leaks are harder for identify the source.
  • 12. Considerations There’re different approaches trying to find the best memory management model. Sometimes you need to chose between performance or memory footprint. Non-deterministic GC is not suitable for RT applications. There’re different tools to track memory leaks (i.e: Valgrind) Automatic memory management doesn’t mean you don’t need to think and understand it.