程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java內存洩露的理解與解決(3)

Java內存洩露的理解與解決(3)

編輯:關於JAVA

附SoftHashmap 的源碼一份,相信看過之後,大家會對 Reference 機制的應用有更深入的理解。

  1. package com. *** .widget;
  2. // : SoftHashMap.Java
  3. import Java.util. * ;
  4. import Java.lang.ref. * ;
  5. import android.util.Log;
  6. public class SoftHashMap extends AbstractMap {
  7. /** The internal HashMap that will hold the SoftReference. */
  8. private final Map hash = new HashMap();
  9. /** The number of "hard" references to hold internally. */
  10. private final int HARD_SIZE;
  11. /** The FIFO list of hard references, order of last Access. */
  12. private final LinkedList hardCache = new LinkedList();
  13. /** Reference queue for cleared SoftReference objects. */
  14. private ReferenceQueue queue = new ReferenceQueue();
  15. // Strong Reference number
  16. public SoftHashMap() { this ( 100 ); }
  17. public SoftHashMap( int hardSize) { HARD_SIZE = hardSize; }
  18. public Object get(Object key) {
  19. Object result = null ;
  20. // We get the SoftReference represented by that key
  21. SoftReference soft_ref = (SoftReference)hash.get(key);
  22. if (soft_ref != null ) {
  23. // From the SoftReference we get the value, which can be
  24. // null if it was not in the map, or it was removed in
  25. // the processQueue() method defined below
  26. result = soft_ref.get();
  27. if (result == null ) {
  28. // If the value has been garbage collected, remove the
  29. // entry from the HashMap.
  30. hash.remove(key);
  31. } else {
  32. // We now add this object to the beginning of the hard
  33. // reference queue. One reference can occur more than
  34. // once, because lookups of the FIFO queue are slow, so
  35. // we don't want to search through it each time to remove
  36. // duplicates.
  37. // keep recent use object in memory
  38. hardCache.addFirst(result);
  39. if (hardCache.size() > HARD_SIZE) {
  40. // Remove the last entry if list longer than HARD_SIZE
  41. hardCache.removeLast();
  42. }
  43. }
  44. }
  45. return result;
  46. }
  47. /** We define our own subclass of SoftReference which contains
  48. not only the value but also the key to make it easIEr to find
  49. the entry in the HashMap after it's been garbage collected. */
  50. private static class SoftValue extends SoftReference {
  51. private final Object key; // always make data member final
  52. /** Did you know that an outer class can Access private data
  53. members and methods of an inner class? I didn't know that!
  54. I thought it was only the inner class who could Access the
  55. outer class's private information. An outer class can also
  56. Access private members of an inner class inside its inner
  57. class. */
  58. private SoftValue(Object k, Object key, ReferenceQueue q) {
  59. super (k, q);
  60. this .key = key;
  61. }
  62. }
  63. /** Here we go through the ReferenceQueue and remove garbage
  64. collected SoftValue objects from the HashMap by looking them
  65. up using the SoftValue.key data member. */
  66. public void processQueue() {
  67. SoftValue sv;
  68. while ((sv = (SoftValue)queue.poll()) != null ) {
  69. if (sv.get() == null ) {
  70. Log.e( " processQueue " , " null " );
  71. } else {
  72. Log.e( " processQueue " , " Not null " );
  73. }
  74. hash.remove(sv.key); // we can Access private data!
  75. Log.e( " SoftHashMap " , " release " + sv.key);
  76. }
  77. }
  78. /** Here we put the key, value pair into the HashMap using
  79. a SoftValue object. */
  80. public Object put(Object key, Object value) {
  81. processQueue(); // throw out garbage collected values first
  82. Log.e( " SoftHashMap " , " put into " + key);
  83. return hash.put(key, new SoftValue(value, key, queue));
  84. }
  85. public Object remove(Object key) {
  86. processQueue(); // throw out garbage collected values first
  87. return hash.remove(key);
  88. }
  89. public void clear() {
  90. hardCache.clear();
  91. processQueue(); // throw out garbage collected values
  92. hash.clear();
  93. }
  94. public int size() {
  95. processQueue(); // throw out garbage collected values first
  96. return hash.size();
  97. }
  98. public Set entrySet() {
  99. // no, no, you may NOT do that!!! GRRR
  100. throw new UnsupportedOperationException();
  101. }
  102. }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved