Sometimes in small projects Im using voltage regulators with outputs 8V or 5V such as LM7805. Power is I*V but Im wondering when really a heatsink is needed. Sometimes the current flow from the regulator output is 1mA but in an other project 20mA or more. Is there a rule of thumb for when to concern about heating and considering to use a heatsink? Consider operating time is 12 hours.
My personal rule of thumb is that a TO-220 3-terminal linear regulator does not need a heatsink for less than 600mW (vertical mount). That's based on industrial service and high reliability so it's a conservative number.
If it needs to be more than that then I do the calculations, and possibly even tests, and decide what's best.
You can do a moderate copper pour and use a (surface mount) TO-252 and get better thermal performance than a TO-220 without a heatsink - often quoted at 65°C/W in air. That costs nothing except a bit of PCB area- no fasteners, assembly labor or heat sink costs to consider, no extra (secondary) operations and extra ways for assembly workers to screw things up.
In my opinion, if you're close to needing a heat sink for a linear regulator it's time to at least consider a switching supply unless you have special requirements such as low EMI.
The part datasheet will specify thermal resistances. Looking at a LM7805 datasheet from Fairchild (just the first one to pop up in a search) thermal resistance is 5C/W junction to case, and 65C/W junction to air.
Max operating temperature is 125C. If the device is in a 25C environment, you can handle 100C rise (though some things fall off at Max Operating Temperature, typically) so about 1.5W (100C/65C/W) will be all you can dissipate without a heat sink. If your input supply is 30V, that's perhaps 60mA; if the input is 12V it's more like 214 mA; if your input is 8V, it's 500mA. (All at 25C ambient.)
Working the example the other direction, your comment indicates a 24V supply and a dissipation of 0.38W;
EDIT: In all of the above I have evidently neglected the regulator's own current use of 5.5-6mA (at full input voltage), located a long way down the datasheet, which adds to the power load; in your 24V example this would lower your ambient safe range to about 90 C
When you add a heat sink, you will have the 5C/W junction to case (thermal resistance) plus a certain amount of thermal resistance from the case to the heatsink (influenced by thermal grease, insulators or not, etc.) and finally the heatsink's more favorable resistance to air.
So if the interface is 2C/W and the heatsink is 10C/W, you'd have a total of 5+2+10=17C/W from junction to air with interface and heatsink.
With the 7805 having a grounded case/tab, it's easy to bolt it to the side of a metal case/chassis for some "free heatsinking" if desired. When prototyping, if using parts with thermal overload protection (which is claimed but not fleshed out in the datasheet) you can "just see if the part gets hot and shuts down" though you should run the numbers before finalizing the design, especially since prototypes often have better natural convection than a finished product.
The power dissipated is the voltage drop i.e. Input voltage - output voltage * the input current.
The data sheet will state the maximum junction temperature, and a thermal resistance to the case in °C / W This will be the temperature rise from ambient (so if you are operating in the tropics you have a higher ambient) if the device is in free air without a heat-sink
Provided the calculations give a junction temperature less than max, you don't need a heat-sink
Operating time is not usually an issue - unless your application will only operate for a few seconds, when the thermal lag will lead to a lower rise at the junction.
1) Calculate the power dissipation in the LDO. E.g. 8V to 5V at 100 mA is 0.3 W (voltage difference times the average current).
2) Calculate the temperature rise to air (no heatsink): 65 °C/W * 0.3 W = 20 °C
3) Look at the worst ambient operating temperature: e.g. ~ 40 °C
4) The max case temperature will be 40 °C + 20 °C = 60 °C, which is below the max operating temperature.
Notice that this temp rise is quite small for this very small voltage drop. That's why a heatsink is typically recommended for heavier currents or larger voltage drops. I'd say that maybe 10% of LDO's that I have seen in total have a heatsink.