Your SlideShare is downloading. ×
0
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Memory management
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Memory management

780

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
780
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Memory Management Kuban Dzhakipov @Sibers 2012
  • 2. Plan● introduction● fundamentals● solutions via WeakReference, SoftReference, etc● analyze tools
  • 3. Memory Usage8 primitive data types:Integers: ● byte ● short - 2 bytes ● int - 4 bytes ● long - 8 bytesFloating-point number: ● float 4 bytes ● double 8 bytesCharacters: ● char - 2 bytes (utf16)Boolean ● boolean - 1 byte (1 byte versus 1 bit, alternative BitSet)
  • 4. Memory UsageObject Shallow size = object header + object fieldsObject header: ● Hash Code ● Garbage Collection Information ● Type Information Block Pointer ● Lock ● ArrayLengthobject header size:* 32bit - 8 bytes* 64bit - 16 bytes
  • 5. Memory Usage 1. public final class String { //8 bytes 2. private char value[]; // 4 bytes 3. private int offset; // 4bytes 4. private int count; //4 bytes 5. private int hash; //4 bytes 6. } 7. 8. // Shallow size of a String = 24 bytes 9. // Davlik VM for ARM10.11. * 32 bit architecture
  • 6. Memory Usagenew String() // 24 bytesnew String("a") // 40 bytesnew char[1]Object Header: 12 bytesData type char: 2 bytesOffset : 2 bytesTotal: 16 bytesnew String("a") == 40 bytes* 32 bit architecture* 64 bit architecture = 40+16=56bytes
  • 7. GC
  • 8. GC 1. for(int z=0; z < 10000; z++){ 2. SomeClass a = new SomeClass(); 3. a.doSomething(); 4. for(int i=0; i<100;i++; i++){ 5. StringBuilder str = new StringBuilder(); 6. str.append("This is #"); 7. str.append(i); 8. System.out.println(str.toString()); 9. }10. }11. .......12. // collects objects by gc
  • 9. Memory Leak 1. // example #1 2. ArrayList least = new ArrayList(); 3. for(int i=0; i<10000; i++){ 4. String n = "something"; // couldnt be destroyed by gc 5. least.add(n); 6. }....* least is property of object 7. //example #2 8. @Override 9. protected void onCreate(Bundle state) { 10. super.onCreate(state); 11. 12. TextView label = new TextView(this); 13. label.setText("Leaks are bad"); 14. 15. setContentView(label); 16. }
  • 10. Memory Leak // strong reference
  • 11. OutOfMemoryError?
  • 12. Solutions● SoftReference● WeakReference● PhantomReference
  • 13. SoftReference 1. SoftReference<List<Foo>> ref = new SoftReference<List<Foo>> (new LinkedList<Foo>()); // create some Foos, probably in a loop 2. List<Foo> list = ref.get(); 3. if (list == null) throw new RuntimeException("ran out of memory"); 4. list.add(foo);
  • 14. WeakReference1. WeakReference<List<Foo>> ref = new WeakReference<List<Foo>> (new LinkedList<Foo>()); // create some Foos, probably in a loop2. List<Foo> list = ref.get();3. if (list == null) throw new RuntimeException("ran out of memory");4. list.add(foo);
  • 15. PhantomReference 1. PhantomReference<List<Foo>> ref = new PhantomReference<List<Foo>>(new LinkedList<Foo>(), new ReferenceQueue<List<Foo>>()); // create some Foos, probably in a loop 2. List<Foo> list = ref.get(); // always return null
  • 16. Memory Analyze ToolJHatJProfilerEclipse Memory AnalyzerYourkitNetbeans
  • 17. Eclipse Memory AnalyzerInformation available onhttp://eclipse.org/mat/
  • 18. Get a Heap● jconsole● jmap● ddms(android)
  • 19. Overview
  • 20. Histogram
  • 21. Dominator Tree
  • 22. Path to GC Roots
  • 23. GC RootsThe so-called GC (Garbage Collector) roots are objects special for garbage collector. Garbage collector collects those objectsthat are not GC roots and are not accessible by references from GC roots.There are several kinds of GC roots. One object can belong to more than one kind of root. The root kinds are: ● Class - loaded class. Classes can hold objects via static fields. ● Alive Threads ● Stack Local - local variable or parameter of method. ● Finalizer Queue Entry - object scheduled for finalization. ● GC Handle - provides a means for accessing a managed object from unmanaged memory. ● Other - objects hold from garbage collection by CLR for other reasons.
  • 24. Summary
  • 25. ?
  • 26. Thanks for your time! Sources: d.android.com habrahabr.ru eclipse.org/mat

×