memory management

13
DANIEL FRANÇA MEMORY MANAGEMENT - COMPARATIVE OF C/C++/JVM/PYTHON.

Upload: daniel-c-franca

Post on 19-Jun-2015

113 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Memory management

DANIEL FRANÇA

MEMORY MANAGEMENT - COMPARATIVE OF C/C++/JVM/PYTHON.

Page 2: Memory management

What’s memory management?

The process of dynamically allocate memory when you need and free when you don’t need it anymore.

Page 3: Memory management

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)

Page 4: Memory management

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 freefree(ptr); //Free memory of ptr

You need to be aware of the ownership of the pointer. (who’s responsible for free the memory)

Page 5: Memory management

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.

Page 6: Memory management

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.

Page 7: Memory management

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.

Page 8: Memory management

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

Page 9: Memory management

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

Page 10: Memory management

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.

Page 11: Memory management

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.

Page 12: Memory management

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.

Page 13: Memory management

Dank u

Vragen?