Let us look as some basic's, these seem to be the basis of a lot of questions:
A capacitor when power is switched on draws a huge amount of current that tapers off as it gets charged. This curve is also called the RC time constant (this is close but not exact "http://www.electronics-tutorials.ws/rc/rc_1.html" will give a better explanation).
When switching off a capacitor discharges at an exponential rate (RC time constant) depending on the value, charge voltage and the load. This makes them good for holding power for a short time when the power fails.
An Inductor draws nothing when first switched on but the current increases exponentially until its voltage reaches the supply voltage.
When switched off the inductive field in the inductor collapse causing the polarity to reverse. The voltage will rise unlimited until typically something externally limits it. The faster it is turned off the quicker the rise time and voltage. Energy will cease to flow when the inductive charge is dissipated. Guess where this current goes when the inductive load such as a relay is connected to a port pin?
For this reason you need to put a diode (commonly called a fly wheel diode) across the inductive load. Google for: "inductor/capacitor charge curve" you will find a lot of nice graphs explaining this. If you look at the circuit it has the cathode + connected to the most positive side of the power supply. In this configuration it will not conduct unless the voltage is reversed (when the inductive load is switched off).
Another common misconception is you can load a microprocessor I/O to the maximum. This is bad design. They give you a maximum per pin, per port and for the chip. At room temperature you will probably get away with it for a while.
Let us assume we have a port with a 40mA load. The output is 0.005 from the power rail. Using Ohm’s law we are dissipating 20 milliwatts of power on one pin. At this rate of loading it does not take long to over heat the device because of internal power dissipation.
When the output pin is changing state it draws more current because it has to charge or discharge its internal and the external capacitance, ‘more heat’, more speed ‘more heat’.
If you look some of the specs will give you a maximum temperature, it is for the junction on the die, not the case temperature. Plastic is a poor conductor so heat sinking the package does not do a lot. Now consider this along with the ambient temperature. The ratings are given typically with the device at 25C, guess what happens when it gets warmer.