Wednesday, March 25, 2015

OS X : Understanding RAM Usage

0. Understanding terminologies 

Left table 

Physical Memory :

        Actual RAM installed on machine ( aka Physical Memory ).

Memory Used :

        Amount of RAM used from physical.

Virtual Memory ( Non physical memory / spaced used from your HDD ) :

       Memory used by VM, includes swap as well as compressed data.

Swap Used :

       Amount of data currently compressed and moved to HDD for freeing physical memory.

Middle graph

Green :

       Good condition, everything should work just fine.

Yellow :

       Resources are not getting amount of memory required to run properly.

Red :

       Out of memory.

Right table

App Memory :

       Physical memory used by applications & system processes.

File Cache :

       Compressed files on physical memory. These are files from recently used application cached for faster access.

Wired Memory :

       This is data which will stay inside physical memory which is neither swappable nor it can be compressed.

Compressed :

       This is data on physical memory in compressed format as it was not used recently.

1. Understanding usage 

Since OS X 10.9, RAM utilisation techniques are completely different, Mavericks & Yosemite try to use all of your physical memory to speed up processes. So it doesn't matter whether you have 4 , 8 or 16 GB of RAM, most of the times you will see "Memory Used :" parameter from screenshot close to your physical memory.  But nothing to worry "File Cache :" & "Compressed" memory allocations will release memory in fractions of seconds if applications need more memory.

Efficient utilisation 

File Cache :

You can release this memory my command "sudo purge".

But, soon your system will start using this freed memory for caching again. This is quick solution for resolving hang like effect, as reaching file cache again to higher value takes some time.

Compressed :

This should be close to zero most of the times, if you see it crossed around 1 GB, restart your machine. Not shutting down or restarting machine for long period of time keeps on adding compressed memory.

Disk Usage :

Keep around 40 GB of free HDD space for smoother operations. Lesser HDD space results into less use of virtual memory, it also slows down read-writes, which affects overall performance of system.

Closing unnecessary processes :

Click on memory title from statistics table, this will sort memory usage from Highest to Lowest order, close all unnecessary user application by targeting high memory using application first.

2. Geek zone

How to disable compressed memory ?

vm/vm_pageout.h defines the modes for the vm_compressor boot argument.
Default value is VM_PAGER_COMPRESSOR_WITH_SWAP ( from vm/vm_compressor.c )

To disable compression change vm_compressor_mode argument to 1 ( VM_PAGER_DEFAULT )

       sudo nvram boot-args="vm_compressor=1"

Restart machine & verify change is successful

       sysctl -a vm.compressor_mode

To re-enable compression change vm_compressor_mode argument to 4 ( VM_PAGER_COMPRESSOR_WITH_SWAP )

       sudo nvram boot-args="vm_compressor=4"

How is memory pressure calculated ?

Memory pressure is defined by two counters Mach keeps internally: 

vm_page_free_count: How many pages of RAM are presently free 
vm_page_free_target: How many pages of RAM, at a minimum, should optimally be free.


sysctl -a vm | grep page_free


vm.vm_page_free_target: 4000
vm.page_free_wanted: 0
vm.page_free_count: 198477

Pressure levels


sysctl kern.memorystatus_vm_pressure_level


kern.memorystatus_vm_pressure_level: 1

level 1 : Normal (Green)
level 2 : Warning (Yellow)
level 4 : Critical (Red)

Changing memory pressure levels


Normal : Graph should change to green levels
sudo memory_pressure -l normal

Warning : Graph should change to steady yellow levels
sudo memory_pressure -l warn

Critical : Graph should change to red levels
sudo memory_pressure -l critical

Other commands 


In detail virtual Memory Statistics.


In detail pages & swap info.

Saturday, March 7, 2015

iOS Development : Working with size classes ( Level : Beginner )

What are size classes ?

Apple is always trying to make developers life easy, every year they introduce something new which let developer achieve their application goals in much easier way. Size classes is solution to developer's question - How can i easily create a view, which will render the way i expect irrespective of type of screen size, type of device & type of orientation ?

Enabling size class

If you create new project in Xcode 6.0 & above, size class is by default enabled, to use size classes, enabling auto layout is must.

To enable size classes, select xib or storyboard file. In the Utilities select "File Inspector", check both options "Use auto Layout" & "Use Size Classes".

Possible Combinations

If you want to perform any device type or orientation specific changes for view, following are possible combinations.


For all devices & orientation.
C0 : Any, Any
If nothing specific done in following combinations (C1-C4), then by default view follows default setup C0.

C1 : Portrait - Regular, Regular
C2 : Landscape - Regular, Regular

C3 : Portrait - Compact, Regular
C4 : Landscape - Compact, Compact ( Except 6+, for 6+ it is Regular, Compact)

Options available for customisation

What all options provided by apple to perform something specific in one of the Combination C0-C4 ?

Option 1 : Font

Setting different font for different combination.

Option 2 : Subview hiding

Not showing some view in some combination.

Option 3 : Constraint

Setting different constraints for each combination.

Option 4 : Xcassets

Using different assets for different combination.

How to achieve it  ?

Rule is simple !!!

Want to do something specific in one of the combination ?

Select your view on interface builder change size class to your required combination from C1- C4. Use option from Opt1-Opt4 to customise view for selected combination.

By default view follows C0 customisation if no customisation is done in combination from C1-C4.

How to change size class ?

By default Any Any size is set, to change it click on text "wAny hAny".

This is how popover appears on click, drag mouse around, when title highlights your required combination click it.

Creating demo applications using options 1 to 4.

Option 1 : Font size demonstration


Use one UILabel which will have different font size on iPhone ( System 15 ) & iPad ( System 40 )


0. Create universal single view application.

1. Go to storyboard, add one label to view, keep it to top left ( use leading & top constraint ) with size around 150x50.
2. Set system font 15, set text "Font Test"

This is how your storyboard view will look like..

3. Now hit "+" sign highlighted in red rectangle in above screenshot

As we want something specific to iPad we will choose combination C1/C2 i.e Regular Width, Regular Height.

Set font 40 for this combination.

4. Run application on iPhone & iPad you will see different font size for respective device type.

Option 2 : Subview hiding


Use device specific view. ( View used for iPhone should not be visible on iPad & vice versa)


We will use two UIView with red & green background colour.

Red view for iPhone & Green for iPad

0. Create universal single view application.

1. Go to storyboard, add two view one with red background colour & other with green, keep it to top left ( use leading, top, height, width constraint ) with size around 100x100.

This is how it should look after setup..

2. Now select Red view, go to attribute inspector, hit "+" sign highlighted in red rectangle

As we want something specific to iPad we will choose combination C1/C2 i.e Regular Width, Regular Height

This will install same view for Regular x Regular combination

Perform same for Green view.

3. Lets hide Red view for Regular x Regular combination ( iPad ), to do this uncheck installed view for wR hR combination. It should look like this

 Now do exact opposite for Green view. It should look like this

The moment you do this Green view will disappear, on left side you will see your Green view greyed out / disabled.  

4. Run application on iPhone & iPad, Red view will be only visible on iPhones & Green will be only visible on iPads.

How can i make changes to green view as it is not visible ?

For this you will have to change your size class to wRegular hRegular from your current mode (which is wAny hAny) from bottom of storyboard. To make changes to Red view switch back to wAny hAny.
Other solution is install views for all combinations, make changes & before running install / uninstall views.

Option 3 : Constraints


Create iPhone application with image view & comment view.

Portrait mode : Image view at top & comment view at bottom.
Landscape mode : Image view at left & comment view to right.

See gif from step 5 for understanding goal.


0. Create iPhone single view application.

1. Go to storyboard, add two views one UIImageView & other simple UIView as comment view.
2. Setup image view at top with some image set to it, set comment view at bottom with some background colour.
3. Setting constraints 
For image view set leading constraint with constant value 0. Now select constraint go to attribute inspector, hit "+" sign highlighted in red rectangle, select compact width & compact height.
This is how it should look after..

Now refer following table to add rest of constraints with installed check / uncheck for respective Combination. 
Note - Give proper name to constraints for better readability.

4. This is how your view constraint should look, once you are done with constraint setup

Storyboard mode - wAny hAny

Storyboard mode - wCompact hCompact

5. This is how your app with orientation will look like

Option 4 : Using different assets for Combinations C0-C4.


1. Go to Images.xcassets

2. Select any image set
3. Open Attributes inspector
4. Change width & height to specific combination C0-C4, this will add more image placeholder other than 1x, 2x, 3x.

5. Add images to placeholder for respective combination.