×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    memory fragmentation

    I have an Object, let's say DataInputStream dis. When I assign it:
    dis = Connector.openDataInputStream(String a)
    then I do it again with another content
    dis = Connector.openDataInputStream( String b)
    - is it proper example of reusing this object, I mean such that allows avoiding memory fragmentation on some phones?
    And what about using 'new' statement, still concerning fragmentation issue:
    dis = new DataInputStream(InputStream a)
    and later
    dis = new DataInputStream(InputStream b)
    ?

  2. #2
    Regular Contributor
    Join Date
    Jun 2003
    Location
    Hungary
    Posts
    414
    These are not "reuse" examples.
    If you have a variable like
    Code:
    DataInputStream dis = null;
    that mean you allocate 4 bytes memory that will act as a reference to an object. Then
    Code:
    dis = Connector.openDataInputStream(String a)
    will allocate a load of memory. If you open another IputStream than it will allocate a huge amount of memory again. Of course you may use the dis variable to reference it, but it is still only 4 bytes.
    An example of object reusing:
    Code:
    public class Zombie {
        int xPosition;
        int yPosition;
        int state;
        public void reborn() {...}
    .....
    }
    
    ....
    Zombie[] myZombies = new Zombie[20];
    for (int i = 0; j < 20; j++) {
        myZombies[j] = new Zombie();
    }
    ...
    public void killZombie(int index) {
        myZombies[index].reborn();
    }
    In that case you don't create new object, just use existing ones. You also have to care with nested objects, like Vector or whatever.

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Oooooohhh, zombies! That was the coolest post evar!!!!

    shmoove

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    I do not see REusing objects in your example, just using them...

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    He is reusing objects because once a Zombie dies (can zombies die?), instead of nulling the reference and then newing up a new Zombie object, he just resets all the variables to their initial status and gets a new zombie without allocating any new memory. So he in fact just reused the same zombie. The big trick in reusing objects to save memory is to try to "new" up the least amount of objects.

    In your example there is no real way to do this since you're using sytem objects and they usually don't have any way to "restart" themselves, but with your own objects you can usually do it quite easily.

    shmoove

  6. #6
    Regular Contributor
    Join Date
    Jun 2003
    Location
    Hungary
    Posts
    414
    Originally posted by shmoove
    Oooooohhh, zombies! That was the coolest post evar!!!!
    I'm working on a game that have zombie enemies, so that was came first in my mind.

  7. #7
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    I'll play that. Love those zombies.

    shmoove

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×