1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package com.vaadin.tests.components.table;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import com.vaadin.data.Container;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.TestUtils;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
public class EditableTableLeak extends TestBase {
private final Table table = new Table("ISO-3166 Country Codes and flags");
private final CheckBox useFieldFactory = new CheckBox(
"Use a caching TableFieldFactory");
private final Label sizeLabel = new Label("", ContentMode.HTML);
private long size = 0;
static class DebugUtils {
private static class ByteCountNullOutputStream extends OutputStream
implements Serializable {
private static final long serialVersionUID = 4220043426041762877L;
private long bytes;
@Override
public void write(int b) {
bytes++;
}
public long getBytes() {
return bytes;
}
}
public static long getSize(Object object) {
ByteCountNullOutputStream os = new ByteCountNullOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(os);
oos.writeObject(object);
} catch (IOException e) {
e.printStackTrace();
}
return os.getBytes();
}
}
private static class CachingFieldFactory extends DefaultFieldFactory {
private final HashMap<Object, HashMap<Object, Field<?>>> cache = new HashMap<Object, HashMap<Object, Field<?>>>();
@Override
public Field<?> createField(Container container, Object itemId,
Object propertyId, Component uiContext) {
if (cache.containsKey(itemId)) {
if (cache.get(itemId) != null
&& cache.get(itemId).containsKey(propertyId)) {
return cache.get(itemId).get(propertyId);
}
}
Field<?> f = super.createField(container, itemId, propertyId,
uiContext);
if (!cache.containsKey(itemId)) {
cache.put(itemId, new HashMap<Object, Field<?>>());
}
cache.get(itemId).put(propertyId, f);
return f;
}
}
@Override
protected void setup() {
addComponent(useFieldFactory);
useFieldFactory.setImmediate(true);
useFieldFactory.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
if (useFieldFactory.getValue()) {
table.setTableFieldFactory(new CachingFieldFactory());
} else {
table.setTableFieldFactory(DefaultFieldFactory.get());
}
}
});
addComponent(table);
table.setEditable(true);
table.setWidth("100%");
table.setHeight("170px");
table.setSelectable(true);
table.setContainerDataSource(TestUtils.getISO3166Container());
table.setColumnHeaders(new String[] { "Country", "Code" });
table.setColumnAlignment(TestUtils.iso3166_PROPERTY_SHORT,
Table.ALIGN_CENTER);
table.setColumnExpandRatio(TestUtils.iso3166_PROPERTY_NAME, 1);
table.setColumnWidth(TestUtils.iso3166_PROPERTY_SHORT, 70);
addComponent(sizeLabel);
addComponent(new Button("Show size of the table", new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
table.markAsDirtyRecursive();
updateSize();
}
}));
addComponent(new Button("Select the second row", new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
table.select("AL");
updateSize();
}
}));
}
private void updateSize() {
System.gc();
long newSize = DebugUtils.getSize(table);
sizeLabel.setValue("Size of the table: " + newSize
+ " bytes<br/>Delta: " + (newSize - size));
size = newSize;
}
@Override
protected String getDescription() {
return "Table leaks memory while scrolling/selecting when in editable mode";
}
@Override
protected Integer getTicketNumber() {
return 6071;
}
}
|