Skip to content

Instantly share code, notes, and snippets.

@Dunemaster
Created June 2, 2015 12:25
Show Gist options
  • Save Dunemaster/5c3661f2a96c00f521d6 to your computer and use it in GitHub Desktop.
Save Dunemaster/5c3661f2a96c00f521d6 to your computer and use it in GitHub Desktop.
public class MyViewController extends UIViewController {
public class TestClass {
public int integerValue;
public String stringValue;
public TestClass referenceValue;
public TestClass referenceValue2;
public int[] arrayValue;
}
private final UIButton testButton;
private final UIButton clearButton;
private final UILabel label;
private final ArrayList<TestClass> testObjects;
public MyViewController(PersonalFinanceService _financeService) {
testObjects = new ArrayList<TestClass>();
// Get the view of this view controller.
UIView view = getView();
// Setup background.
view.setBackgroundColor(UIColor.white());
//Setup label
label = new UILabel(new CGRect(20, 340, 300, 40));
label.setFont(UIFont.getBoldSystemFont(12));
view.addSubview(label);
// Setup button.
testButton = UIButton.create(UIButtonType.System);
testButton.setFrame(new CGRect(20, 280, 300, 40));
testButton.setTitle("Test 100k objects", UIControlState.Normal);
testButton.getTitleLabel().setFont(UIFont.getBoldSystemFont(22));
testButton.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {
@Override
public void onTouchUpInside(UIControl control, UIEvent event) {
int capacity = 100000;
// COMMENT LINE BELOW TO CAUSE MEMORY FRAGMENTATION
testObjects.ensureCapacity(capacity);
for(int i = 0; i < capacity; i += 1) {
TestClass currentObject = new TestClass();
currentObject.integerValue = i;
currentObject.stringValue = "String" + i;
if(i > 0)
currentObject.referenceValue = testObjects.get(i - 1);
if(i > 1)
currentObject.referenceValue2 = testObjects.get(i - 2);
currentObject.arrayValue = new int[100];
testObjects.add(currentObject);
}
label.setText(String.valueOf(testObjects.size()));
}
});
view.addSubview(testButton);
// Setup button.
clearButton = UIButton.create(UIButtonType.RoundedRect);
clearButton.setFrame(new CGRect(20, 300, 300, 40));
clearButton.setTitle("Clear", UIControlState.Normal);
clearButton.getTitleLabel().setFont(UIFont.getBoldSystemFont(22));
clearButton.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {
@Override
public void onTouchUpInside(UIControl control, UIEvent event) {
testObjects.clear();
testObjects.trimToSize();
System.gc();
}
});
view.addSubview(clearButton);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment