Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some
Accepted Solution

Manager replace causing memory leaks?

Hey folks!

Recently , I've discovered that using 

net.rim.device.api.ui.Manager.replace(Field arg0, Field arg1) causes free memory to go down..
Anyone experiencing this?
Please find below the ready to use example:
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class TestManagerApp extends UiApplication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TestManagerApp().enterEventDispatcher();

	}

	public TestManagerApp() {
		pushScreen(new TestManagerScreen());
	}

	static class TestManagerScreen extends MainScreen {
		LabelField memoryLabel;
		LabelField tabOne;
		LabelField tabTwo;
		ButtonField toggleSwitch;
		VerticalFieldManager body;
		Field currTab;
		Field[] tabs;
		int index;
		static int t = 0;

		public TestManagerScreen() {
			memoryLabel = new LabelField();
			tabOne = new LabelField("TAB ONE");
			tabTwo = new LabelField("TAB TWO");
			body = new VerticalFieldManager();
			currTab = tabOne;
			tabs = new Field[] { tabOne, tabTwo };
			toggleSwitch = new ButtonField("eat switch", ButtonField.CONSUME_CLICK);
			toggleSwitch.setChangeListener(new FieldChangeListener() {

				public void fieldChanged(Field field, int context) {
					Field next = getNextTab();
					body.replace(currTab, next);
					currTab = next;
					updateMemoryLabel();
				}
			});

		
			body.add(currTab);

			add(memoryLabel);
			add(body);
			add(toggleSwitch);

			updateMemoryLabel();

		}

		Field getNextTab() {
			index++;
			if (index >= tabs.length) {
				index = 0;
			}
			return tabs[index];
		}

		void updateMemoryLabel() {
			memoryLabel.setText("Free memory:  " + Runtime.getRuntime().freeMemory());
		}
	}

}

 

 

Got resolved! Press kudo icon!
Highlighted
Developer
Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon

Re: Manager replace causing memory leaks?

[ Edited ]

Yes, I've seen this with the Memory Statistics View in Eclipse.

 

I've been using delete() or deleteAll() with add().

 

Edit: Memory is being freed up, but replace has caused issues and has not correctly deallocated memory when replacing an object of type Field from running invokeLater or invokeAndWait.

 

I'd be happy to be wrong here, but I had to keeping going with delete() or deleteAll() with add().

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 384
Registered: ‎08-12-2008
My Device: 8700

Re: Manager replace causing memory leaks?

Hi,

 

I do not think there is memory leak there,

 

it is just number.

 

I have debug your application and via using memory statics to check.

 

it allocated and it freeed correctly.

 

I am on simulator.

 

Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some

Re: Manager replace causing memory leaks?

thank you guys for your attention. I see that at least one has faced that 

Got resolved! Press kudo icon!
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Manager replace causing memory leaks?

Agree with Jeff. No leak here.  Just that the garbage collector has not run.  Using a debugger break point to force a garbage collection to run, restores the free memory.   The garbage collection will run eventually even if not forced. 

Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some

Re: Manager replace causing memory leaks?

ok, guys. Looks like I can rely on RIM's implementation in this case. Thanks for your attention.

 

Got resolved! Press kudo icon!
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Manager replace causing memory leaks?

Can you mark this as Solved then?