Java: ���������� ���������
��� � �������, ������, ���������� ��������������� �� Java, �� ������ ����� ������������ ����, ��� ������� ��������� ��������� �� ���� �����. � ������ ������ � ��������� �������� ��� ������� �� ���� ������ ����������������.
����������:
- ��� �����, � ���� �� ����� GC.
- ������������ ���. � ��� ������?
- Memory Leak � Java � ��� � ��� ��������.
- ����������� ��������.
��� �����, � ���� �� ����� GC.
����� �� ������������ ����� Java �������� �������������� ������ ������ - ����������� Garbage Collector (���������� - GC). GC - ��� ������� �������, ������� ���������� ������� � ������������� �������������� ��������. ����� ����� ���� ������� � ����������, ������ ��� ��� �����, ������ ������������ ��������� ���� ��� � �����������, ��� � � ����������� ���� ����������. �� � �� �� ����. ������ ������������� GC ��� �������� � ���������� ����������� ���������������� � ������ �����.
����� �� ��������� ������� GC (� ���������� ���������� �������� � �����) �������� ���������� ������������, ����������� ������������ � C++ - ������������� ���������� ������� ��� ����������� �������� �� ������ �� ������� ���������. ������ ��� ���� ������ ���������, ������� ��������� �������� ���� �� ������.
������ ���������� �������� ������������� ������� �� �������� �� �������, ������ ��� GC ������ �� ������� ������ �� ������ ���������� ��� ��������. ��� ����������� ���������� ���������� ������ � JDK 1.2 ��� ����� ����� ����� java.lang.ref, ������ �������� ��������� ��������� ��� ���������� "������" ������ �� �������. ��� �������� ������� ��� ���������� ����������� ��������.
����������� ��������� ������, ����� ����������, ��� �������� �������� ������ �� ����� ���������� - �� ������� GC. ��� ����� ��������, ��� � ������� �������� ������ �� ������ ����������� ������, �� ��� �� � ������ ������. �������� - �������� �����, ������, ResultSet-� � Statement-� � JDBC, � �.�. � �������� ������ GC � ��������� JVM ����������� �������� �������, �� ��� ������������ ������ ��������, ��� �������, ������ ������ ����������. � ��������� ������ ����� �������� ��� ���������� "���������" ������, ������� �� ����������, �� ��������.
��� ����������� ��������� ������ GC �������� ����� ������� ������������, ��� ��� ������ ��� ��������� ����� ���������� ��������� ��������. � ���������, �� ����� ����������� ������� � ������������ �������, ����� �� ��������� � �������� �� �������� ��� ������� ��������� ������ �� ���. ��� ��, ��� �������, � JDK 1.3, GC �������� ����������� ������� ������ �����, ����� ������� ��� ������ �����������. ��� ���� ������������, ���� ��� �����������, ���� �� ����� �� ��������� �������� ������. ������������ ����������� ����� ���������� ������� � ������ �������. � �.�. � �.�.
�� ��������� ������� ����� ���� ������� �� �������. � JDK 1.3 �� ����� 64 ����������. � ��������� ������� ����� ������� ��� ������, �������� ����� �� ������ ������ �� ����� ����� - ��� ����� ��������� ������� ����� �� ����� ������ � ������ ������������ ������ ��������.
����������� JVM �� ������ ������� ������� ������, ������� ��� ������� ��� ���. � ���� ������ ���� JVM, ������� ��� ������ - ��� Microsoft JVM. ��� ��������� ������ �� ���������� ������.
������������ ���. � ��� ������?
������ ������������ (�������� ������ ��� ���������� � C++), ����� ������ ������������ � ���� ������� finalize. ��� �� ���� ���������. �� ������, ���� ����� �� ���������, ���� �� ������ ������� ������. ��� ��������, ��� �� ��������� �����, ����� ��� ��� �����. �� ����� ������ ������� �� ���������. ����, �������, ����� System.runFinalizersOnExit(), �� �� ��� ����� deprecated ��-�� ��������� deadlock-��. ��� ����������� ������� Bruce Eckel � ������������� ������ Thinking in Java ����� finalize ����� ������������ ������ ��� ����, ����� ���������� ������ (��� ������, �� ����� ��������� ������), ���������� � native �������. ��� ������ �� �������� ������� �������� ����� ����� ������ ������� �������� (�� ����� ���������� ��-������� - close, dispose, cleanup, � �.�.), ������ ����������� finally. ������ finally - ��� ����, ����� �� ��������� �����-������ ����������. ������ �������� �����������:
OutputStream out = new FileOutputStream(...);
try
{
...
}
finally { out.close(); }
PreparedStatement stmt = ...;
try
{
stmt.setInt(1,...);
ResultSet rs = stmt.executeQuery();
try
{
...
}
finally { rs.close(); }
}
finally { stmt.close(); }
���� �� ������ ������ ��������� � ����� �����, �� ������ �������� ������� ��� �������� - ��������, � ��� �������� �� ��� ������. :-)
Memory Leak � Java � ��� � ��� ��������.
�������� �� - ����� ����� ���� Memory Leak (������ ������), ����� ���� GC. ��, ��� �� �����, ��� ��������. ��� ������ ���������� �� ������ ������������� �� ������ � C/C++ (����� � Java ������ ���� �� �����, �� ����������� ��������� ������ � JVM, �� ���� ������� �� ����� �� �������������). ��� ��������� ��-�� ������� ������.
�������� - � GUI ����� ������������ �������� ���������� (Listener-��). ��� ����� ���� ������ �������������� � ������ ������� ��� ��������� ������ �� ����������� ������-������ �������. �������� ��� ����� ���� ���� - ���� �� �������� ����������������� ������, ����� �� ���������� ��������. ��� ���� �� ��������� ��������� � ������, �� � GC, ����������, �� ����� ������� ���� ������, ���� �� ����� ����� ������� ������ �� ������� (� ����� ����� � �� ��������� �� ���������� ���������).
� ���������� - ������ ����� �������, �������, ��������� �������� �� ��������� � ���������, �, � ����� ������, �������� OutOfMemoryError.
��������� ����� �������� ����� ��� ������ ����������� �������� - �����������. ����� ��������� �� ������ ������ - JProbe � OptimizeIt.
�������� � ����� ������ �������� ������ - ���� �� ������ �������������������. ���� �� �������, ��� ������-��������� ��������� ������ � �������� - ���������� ������� (��������, ����� ��� ��� ����� ������ �������), �� ����� ������ �� ����� - GC ��� � ���� ���������.
����������� ��������.
����������� �������� ����������� ��� ��������� ������������������. ����� ������ �������� ����� (��� ������ �� �������� � �������� ������ ���������), �� ����� ������� ��������� ��� ���� ���, � ��������� �� ���� ������. �� ������ - ��� ������������ ����� ���������� ������ GC, � ������, ���� ������ ������� �� ����� ������ GC (���� ���� �� ������ �� �����). ������ ��� �������� �������� �� ����� (������ �����, ��� ��� ������� ��������� ����� �����), �� ���� ���������� ��-���� ����� ����������� ��������� �������������� �������.
� JDK 1.2 ��� ���� ����� ���� ��������� ��������� �������, ����� ���������� �� ��� - WeakReference � SoftReference. Weak ("������") ������ ����� �������, ���� �� ������ ��� ����� "�������" ������ � ������ ������� ��� ���� GC. Soft ("������") ������ ����� �������, ���� �� ������ ��� ������� ������, �, � ������� �� ����������� �� ���� ������ ����������� ����� (������������ �������������� � ����������� �� �������� ���������). ��� ����������� ����� ����� �������� ������ ���� Soft, �.�. � ��� ����������� ������� ��������� � �������.
��������������� ������������ ����� �������� �� ����� ������, ����� �������� ��������� � �� ��������������. ���, �������� ���������� ������ SoftHashtable:
import java.lang.ref.SoftReference;
import java.util.Hashtable;
public class SoftHashtable extends Hashtable
{
public SoftHashtable() {}
public Object put(Object key,Object obj)
{
SoftReference ref = (SoftReference)
super.put(key,new SoftReference(obj));
if( ref==null ) return null;
return ref.get();
}
public Object get(Object key)
{
SoftReference ref = (SoftReference)super.get(key);
if( ref==null ) return null;
return ref.get();
}
public Object remove(Object key)
{
SoftReference ref = (SoftReference)super.remove(key);
if( ref==null ) return null;
return ref.get();
}
}
���� ����� �������� ��������, ��� ������� �� ������ ��������� ��������� ������ Hashtable. � ������� ����� ������ �� ��� ������ ����� ������������ ���� ��� ��������:
private static Hashtable objCache = new SoftHashtable();
public SuperObject getObject(String key)
{
SuperObject o = (SuperObject)objCache.get(key);
if( o==null )
{
o = loadObject(key);
objCache.put(key,o);
}
return o;
}
����� �������, ���� GC ���������, ��� ������ ��������
����, � � ���� ��������� �������, � ������� ����� ����� �� ���������, �� ������
�� ���������.