Tuesday, March 27, 2012

File Sizes vs Disk Space Used

This week a friend of mine complained about an Android app consuming much more memory than what he thought should be necessary. It was eating up over 30MB of his memory card space when most comparable apps use far less.

He sent me a copy of the files so I could take a look.

The first thing I noticed was that running the du command on my version of the files showed that they only consumed about 4MB of space. Far less than the 30MB he reported. So why the dramatic difference?

We tend to forget (or at least I do) that filesystems store files in fixed sized blocks (not in bytes).

If the file system's block size is 2K (2048 bytes) than a 1 byte file will physically consume 2048 bytes of disk space. That is space which is no longer available to other files or applications.

The default block size on HFS (OS X / iOS) is 4K (4096 bytes).
My friend had a FAT32 memory card with a block size of 32K (32768 bytes). (*)

Now the files consisted of roughly 1000 images each of which is approximately 1K of data. The total data size then is about 1MB.

But when storing as separate files in whole blocks you get waste which increases as block size increases:

Number of 1K images * block size = total disk space consumed
1000 * 4KB = 3.8 MB
1000 * 32KB = 31.2 MB

Something to keep in mind when considering tradeoffs of how you store data.

References:
(*) According to Microsoft FAT32 partitions that are 32GB or higher have a default block size of 32K. (Note that a block is called a "cluster" in FAT terminology).


Friday, March 23, 2012

My favorite feature in Xcode 4.3


My favorite feature in Xcode 4.3 (LLVM 3.1) is that you no longer need to use forward declarations for private Objective-C methods.

Before this version if you called a private Objective-C method you would get a compiler error if you didn't do one of the following:

1) Move the implementation of the private method above all the calls to it in the source file.
2) Forward declare the method via a category or extension.

#1 sucks because you end up with less readable code. Low level "helper" methods bubble up to the top while the more general methods get pushed to the bottom. Ideally (I think) your source code should be organized and read like a newspaper. The high level concepts / methods should be at the top and the lower level methods on which they depend should be below it.

#2 sucks because the forward declarations are extra work which feel unnecessary and seems to break the DRY principle.

With Xcode 4.3 the restriction has been lifted.

You no longer need to declare private methods and you can call the methods from any where in the source file.

Over the past few years I've been super impressed with how quickly Apple has evolved Objective-C. It's finally starting to look modern. ;-)

My hope for WWDC this year is death to the .h file.

Happy coding.