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
smiley
Posts: 1,359
Registered: ‎07-14-2008
My Carrier: Fido
Accepted Solution

Do i need to free the bitmap after use?

If i create a new bitmap in a local function do i need to free it after exiting the function or does the OS automatically free the bitmap?

 

if I do this does the memory get freed up after exit the function?

 

for (int i = 0; i < 10; i++)

{

    Bitmap b = new Bitmap(...);

}

 

dont see any method in Bitmap to free it.

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008

Re: Do i need to free the bitmap after use?

This is Java...once the reference goes out of scope, or is set to NULL, the garbage collector will (eventually!) free the memory.

 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009

Re: Do i need to free the bitmap after use?

Just to follow up on RexDoug's comment. Like he says, garbage collection happens automatically in Java and there isn't an inverse to the 'new' operator. However, there are a few cases where you need to clean up when you are finished with an object instance. Examples include:

  • I/O streams need to be closed
  • Ditto for Connection objects (and I/O streams from Connection objects need to be separately closed)
  • RecordEnumeration objects need to be 'destroy'ed

In each of these cases, an object ties up, in addition to some memory, a system resource that is in short supply (file handle, etc.).

 

Theoretically, such system resources could eventually be freed when the object is garbage-collected. However, garbage collection does not happen on a definite schedule and any particular piece of garbage might not be collected for a very long time. Besides, RIM removed (more accurately, never included) the Object.finalize() method, which was the hook for making that happen.

Unlike Swing or AWT, Graphics objects (either the lcdui or rim version) that you create do not need to have their dispose() method; in fact, neither the version of Graphics has that method.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ekke
Posts: 1,314
Registered: ‎04-08-2010
My Carrier: Telekom.de, O2, Vodafone

Re: Do i need to free the bitmap after use?

I have some bitmaps where I know that I use them from different places in the app.

 

so from a ResourceFactory  I'm doing it this way:

 

private static Bitmap b;

 

public static Bitmap getBitmapXXXXX() {

  if (b == null) {

    b = Bitmap.getBitmapResource....

 }

return b;

}

 

what do you think - is this ok - or should I create the Bitmap every time new ?

perhaps Bitmap.getBitmapResource has some caching or so ?

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009

Re: Do i need to free the bitmap after use?

I'd guess that getBitmapResource does not cache. As to whether you should cache as you are doing, it's a classic tradeoff between memory and speed. If you're reusing a bitmap a lot, and it's not too large, and your app isn't a big memory hog, then cache. If you use a particular bitmap one or twice during a particular program execution, and it's a huge bitmap, and you're already using huge amounts of memory, don't cache. Everything in between is a judgment call.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ekke
Posts: 1,314
Registered: ‎04-08-2010
My Carrier: Telekom.de, O2, Vodafone

Re: Do i need to free the bitmap after use?

thx Ted,

 

exactly how I'm doing this.... 

only wasn't sure how those resources were handled from BlackBerry, because I'm new to the BB environment

 

I'm also caching Screens where I know they were used many times, sometimes grouping them to have them cached while the user is in a specific part of the app.

 

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009

Re: Do i need to free the bitmap after use?

[ Edited ]

Unless it's a lot of work to build a screen, it's probably not worth caching them. You can only tell by testing on an actual device, because the simulator doesn't simulate the computing power of a device.

 

Edit: I'd tend to err on the side of conserving memory for all of these decisions, unless the performance hit is significant to the user experience.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ekke
Posts: 1,314
Registered: ‎04-08-2010
My Carrier: Telekom.de, O2, Vodafone

Re: Do i need to free the bitmap after use?

thanks...

 

I have separated some parts in library projects - also the resources are in a separate library.

so I'll do some tests with different libraries - one using caching and one not to see if the app has a different 'feeling' for the user

 

I read in other messages / articles that its important to re-use objects and not always create new ones, so from beginning I decided to cache where it may help. but perhaps I'll do some changes here and implement some caching strategies I can switch on / off

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.