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: 107
Registered: ‎10-06-2010
My Device: Not Specified
Accepted Solution

Memory optimisation with multiple Screens

Hi,

 

I have multiple screens in my application and each screen has few custom controls and bitmap images.

 

When I move from one screen to an another screen, I do not need old screen anymore. What is the best way to completely dispose the pervious screen? I would also like to dispose all objects created by that screen as well.

 

I had a deep look into the forums and books but I cannot find or think of any good solution.

 

Your help will be very much appriciated.

 

Thanks

Nitin

Highlighted
Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: Memory optimisation with multiple Screens

you can use deleteAll() function to delete all object when you pop the screen from the stack. And assign null to that object.

Developer
Posts: 107
Registered: ‎10-06-2010
My Device: Not Specified

Re: Memory optimization with multiple Screens

Thank you Aman,

 

My main concern is memory usage.

 

Even if I set the previous screen to null, the screen object does not release the memory. When I see the object viewer, I could see all previous screens and their controls still occupying memory.

 

Is that any idea how can I release memory here? Just FYI, I ran the GC to check if it removed the main object.

 

Thanks

Nitin

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Memory optimization with multiple Screens

popscreen and discard all references (if any).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 107
Registered: ‎10-06-2010
My Device: Not Specified

Re: Memory optimization with multiple Screens

Thanks Simon,

 

That's what I have been doing - popScreen and discarding all references (setting them to null).

 

But when I debug, the discarded objects are still appearing in the object viewer. I am not sure if something wrong with the Object Viewer?

 

Nitin

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Memory optimization with multiple Screens

java garbage collection occurs kind of when it wants, you cannot influence it directly, only suggest that it should run. if you remove all references to an object it is marked for GC and will be removed when GC runs.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 107
Registered: ‎10-06-2010
My Device: Not Specified

Re: Memory optimization with multiple Screens

[ Edited ]

I am aware of the fact that GC runs when he wants! but there is a button to run GC manually from the Object View window. I ran it manually to check if the objects are being removed. But unfortunately not.

 

Now just to make sure - if I set the object to null, will all the references to that object be removed automatically?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Memory optimization with multiple Screens

Setting an object to null is not the same as removing all references - setting a reference to null only removes that one reference.  

 

For example, if you create a Field, add it to a manager, then null the reference to the Field, then your code no longer references the Field.  However the Field is still referenced by the Manager.