Transcript
Page 1: Towards A Light-Weight Tool for Locating Unintentional Object Retention in Java Programs

Towards A Light-Weight Tool for Locating Unintentional Object Retention in Java Programs

MotivationMotivation

• Even with automatic garbage collector, memory leaks do occur in Java programs in the form of loitering objects.

• Such loitering objects can be hard to locate and fix in a complex Java program.

• Current tools for diagnosing Java memory leaks address this problem mostly by digging information buried in JVM heap. This approach is weak at

▫ assisting programmer to establish strong hypothesis about loitering objects.

▫ providing effective clues to where in the source code these loitering objects are retained.

ObjectivesObjectives

• Build a light-weight tool for▫ Identifying the suspected classes of

loitering objects, and further.▫ locating unintended object references in

the source code.

Key IdeasKey Ideas

• The object Destruction/Creation ratio of a class is a good indicator for identifying loitering objects.

• A log of reference assignments that keeps track of all the references linking to the suspected objects will enable us to locate where those unintentional references are set.

• AspectJ’s code weaving facility provides a good basis for building a light-weight tool for the tasks above.

Major StepsMajor Steps

To locate unintended object references:

1. Weave in the object life monitoring aspect into the target application. Run the application for a period of time in order to calculate the D/C ratio value for selected application classes.

2. Identify some the suspected classes based on the D/C statistics, and then weave in the reference logging aspect on objects of those chosen classes.

3. Likewise, run the application for a period of time to log all references linking to the objects of suspected classes.

4. Analyze the reference log and extract reference patterns that look suspicious.

5. Use the reference patterns and precise source code location information to locate the code causing unintended references.

Work AheadWork Ahead

• Investigate enhancements to the scope of AspectJ’s join points.▫ AspectJ does not pick out join points of

array-element access.

• Develop a visual interface for the reference analysis tool.

Chin-Hung ChienFoxconn Electronics [email protected]

Kung Chen and Ju-Bing ChenDept. of Computer ScienceNational Chengchi [email protected]@cs.nccu.edu.tw

Code Weaving in AspectJ Code Weaving in AspectJ

where (pointcut)

do what (advice)aspect

crosscutting concernclass class

weaving

testCases.Queue : 0/1 = 0.0% testCases.QueueNode : 0/100 = 0.0%

Destruction/Creation Ratio

suspected classsuspected class

class : testCases.QueueNode reference count : 110 class statistics : testCases.Queue : 2 testCases.QueueNode : 108 field statistics : QueueNode testCases.QueueNode.next : 9 QueueNode testCases.QueueNode.prev : 99 QueueNode testCases.Queue.headNode : 1 QueueNode testCases.Queue.tailNode : 1 location statistics : Queue.java:22 : 1 Queue.java:46 : 1 QueueNode.java:42 : 9 QueueNode.java:16 : 99

Reference Statistics from the Log

A Flawed Implementation of Queue with Loitering Objects

Execution Flow

myObj = myQueue.dequeue();

myObj = myQueue.dequeue();

myObj = myQueue.dequeue();

myObj = myQueue.dequeue();

Live Object

Loitering Object

Object field

Intended Ref.

Unintended Ref.

myQueue = new Queue();…

myQueue.enqueue(..);myQueue.enqueue(..);

myQueue = new Queue();…

myQueue.enqueue(..);myQueue.enqueue(..);

QN1----------------

QN prevQN next

Object elem

QN1----------------

QN prevQN next

Object elem

QN2----------------

QN prevQN next

Object elem

QN2----------------

QN prevQN next

Object elem

QN3----------------

QN prevQN next

Object elem

QN3----------------

QN prevQN next

Object elem

headhead

......

object Aobject A object Bobject B object Cobject C

QN1----------------

QN prevQN next

Object elem

QN1----------------

QN prevQN next

Object elem

QN2----------------

QN prevQN next

Object elem

QN2----------------

QN prevQN next

Object elem

QN3----------------

QN prevQN next

Object elem

QN3----------------

QN prevQN next

Object elem

headhead

......

object Aobject A object Bobject B object Cobject C

myObjmyObj

QN1----------------

QN prevQN next

Object elem

QN1----------------

QN prevQN next

Object elem

QN2----------------

QN prevQN next

Object elem

QN2----------------

QN prevQN next

Object elem

QN3----------------

QN prevQN next

Object elem

QN3----------------

QN prevQN next

Object elem

headhead

......

object Aobject A object Bobject B object Cobject C

myObjmyObj

public class TestQueue { public static void main(String[] args) { Queue myQueue = new Queue();

for (int i=1; i<=100; i++) myQueue.enqueue(new Integer(i));

for (int i=1; i<=90; i++) System.out.println(myQueue.dequeue()); }}

public class Queue { ... public Object dequeue() { QueueNode tmp = head; head = tmp.getNextNode(); tmp.setNextNode(null); return tmp.getContainedObject(); }}

Test Program

missing code: head.setPreviousNode(null)

;

missing code: head.setPreviousNode(null)

;

Tool ComponentsTool Components

• Object life monitoring aspect▫ calculates D/C ratio

• Reference logging aspect▫ tracks unintended references

• Reference pattern analysis tool▫ extracts suspicious reference patterns

Reference Count:: 1Field: headNodeLocation: Queue.java, line 22

Reference Count:: 1Field: headNodeLocation: Queue.java, line 22

Reference Pattern Extraction

Reference Count:: 1Field: tailNodeLocation: Queue.java, line 46

Reference Count:: 1Field: tailNodeLocation: Queue.java, line 46

Reference Count:: 99Field: prevLocation: QueueNode.java, line 16

Reference Count:: 99Field: prevLocation: QueueNode.java, line 16

testCases.QueueNodetestCases.QueueNode

testCases.QueuetestCases.Queue

Reference Count:: 9Field: nextLocation: QueueNode.java, line 42

Reference Count:: 9Field: nextLocation: QueueNode.java, line 42

What is a Memory Leak in Java

Unreachable

Reachable but not Live

Reachable and Live

Root Set

loitering objects

garbage