×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Aug 2003
    Location
    Eire
    Posts
    182

    static final : would hardcoding save mem?

    static final int Exploding = -2;
    static final int eLavaBall1 = 0;
    static final int eLavaBall2 = 1;
    static final int eFighterPlane = 2;

    I have loads of these globals in my code!

    I've hit the dreaded mem problems on the S40 phones so i'm trying to free up memory everywhere in code and images!

    So i was wondering would removing all od these globals and hardcoding in the values make and difference even if it is small?

    I know the compiler subs in the value when compiled but if there is any benefit regards freeing heap memory i will take it?

    Thanks
    Brian

  2. #2
    Regular Contributor
    Join Date
    Aug 2003
    Location
    Eire
    Posts
    182
    Another Q?

    if i had 4 files all

    30*131 pixels.

    They are all different sizes on my PC.

    When they are loaded by the KVM on a nokia will they all be the exact same size?

    So if i had 4 end of level characters and for each new level i overwrote the image that contained the previous levels end of level baddie they would fit nice into the heap space and i would not have to worry about fragmentation?

  3. #3
    Regular Contributor
    Join Date
    May 2004
    Posts
    147
    Loaded images should take about number_of_pixels*bytes_per_pixel (which is 2 bytes for 12-bit phones).

    I don't think the compiler inserts static finals as literals, despite some documentation suggesting the contrary. Instead, I use a C preprocessor and #defines.

    ehud.

  4. #4
    Regular Contributor
    Join Date
    May 2004
    Posts
    147
    I think loaded images take up around (number_of_pixels * bytes_per_pixel). bytes_per_pixel would be 2 for 12-bit phones, I suppose.

    About static finals, I don't think they are inserted as literals by the compiler, despite some documentation suggesting the contrary. Instead I use a C preprocessor and #defines.

    ehud.

  5. #5
    Regular Contributor
    Join Date
    Jun 2003
    Location
    Hungary
    Posts
    414
    I'm almost sure that static final isn't substituted, because of static attributes may accessed from other classes, but I guess it will work if you declare as private. Try to compile a class without obfuscation, then decompile to see how compiler works.

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Something that's declared final will be substituted with its literal value at compile time if (and only if) it is initialized with a literal value (or an expression that can be resolved to a literal at compile time).

    For example: the literal value for a case must be determined at compile time, so you can do this:
    Code:
    public static final int FISH = 0;
    public static final int DOG = 1;
    
    switch (animal) {
        case FISH:    // will get replaced with 0
            break;
        case DOG:     // will get replaced with 1
            break;
    }
    And you can also do this:
    Code:
    public static final int FISH = 0;
    public static final int DOG = FISH + 1;
    
    switch (animal) {
        case FISH:
            break;
        case DOG:
            break;
    }
    But you can't do this:
    Code:
    // this is legal...
    public static final int FISH = getNextEnumerationValue();
    public static final int DOG = getNextEnumerationValue();
    
    // ...but this isn't, because the "constants" are initialized from method calls
    switch (animal) {
        case FISH:
            break;
        case DOG:
            break;
    }
    As for the images, yes, if they have the same pixel dimensions, their memory footprints will be the same. Whether they get put in the same memory slot depends on what other memory is free at the time, so no, it doesn't guarantee you to avoid fragmentation.

    Graham.

Posting Permissions

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