Proper use of the .New methods, out of memory error

Discussion in 'Questions (Windows Mobile)' started by kolbe, Sep 5, 2008.

  1. kolbe

    kolbe Active Member Licensed User

    Hello all,

    I have a program that uses a few bitmap objects. I see that the program keeps on allocating memory as it runs. When I change the bitmaps I call the New methods again.

    So the question is do the new methods allocate new object memory each time or does it just reinitialize the object and it's memory. I've been under the impression that it just reinitializes the object. Should I be just changing the .value method and avoid calling New over and over. This goes for all objects actually.

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The .Net garbage collector should free unused memory.
    Do you get an out of memory exception?
    It is possible that the bitmaps are still referenced somewhere in your program and therefore their resources will not be freed.
    In that case it is better to use Value instead of New which creates a new object.

    You could also manually dispose the object before creating a new one:
    Code:
    Bitmap1.Dispose
    AddObject(
    "Bitmap1","Bitmap")
    Bitmap1.New1(...)
     
  3. kolbe

    kolbe Active Member Licensed User

    Hi Erel,

    Yes eventually I get a out of memory exception. I can see that the memory allocated by the program steadily increases. The gccollect method helps but the allocated memory still increases.

    I have three bitmaps objects that I continually use in the program. The bitmap continually get changed. At the moment I call new each time I need to change the bitmaps. The bitmap is "on" the screen when new is called. I don't create any additional bitmap objects just reuse the three I already have.

    So, if I follow, because the bitmaps are being used/referenced in the program a dispose and addobject is needed to free all memory.

    Is this advice for all other objects as well?

    Thanks
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    GCCollect should not be used. The garbage collector will free unused memory from time to time or when free memory drops below a certain value.

    Did you try to assign the Value property instead of calling New?
     
  5. kolbe

    kolbe Active Member Licensed User

    In some places I load the image from a file. To use the value method it seems to me that I need to then make a imagelist but I don't particularly care to do this because the images are background images and can be many. I can try just to see the result.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    The Dispose / AddObject is a good solution.
    You should not use it with most objects that unless you repeatedly create and destroy such objects.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice