A battery of emf \(V\) that fully charges an uncharged capacitor \(C\) does work \(CV^2\). The capacitor keeps only \(\frac{1}{2}CV^2\). The other \(\frac{1}{2}CV^2\) becomes heat in the resistance of the circuit, and this is true whatever that resistance is.
Work by the battery
The battery moves the charge \(Q\) through its emf \(V\), so it does work \(W = QV\). A full charge is \(Q = CV\), so \(W = CV^2\).
The emf stays \(V\) the whole time.
Energy stored
A capacitor with charge \(q\) stores \(U = \frac{q^2}{2C}\). When full, \(U = \frac{1}{2}CV^2 = \frac{1}{2}QV\).
Half of \(QV\), because the capacitor's voltage grows from \(0\) to \(V\) as it fills.
Where the energy goes: one graph
A 10 V battery charges an uncharged 2 \(\mu\)F capacitor through \(R\), so \(Q = CV = 20\ \mu\mathrm{C}\) flows. On a graph of voltage against the charge \(q\) so far, an area is an energy (voltage times charge).
The battery pushes every bit through its full 10 V: the rectangle, \(QV = 200\ \mu\mathrm{J}\).
The capacitor takes each bit at its voltage then, \(V_C = \frac{q}{C}\), rising from 0 to 10 V: the triangle, \(\frac{1}{2} \times 20 \times 10 = 100\ \mu\mathrm{J}\), half the rectangle.
The gap between the lines is \(V_R = V - V_C\) (Kirchhoff's loop rule), so the upper triangle, the other \(100\ \mu\mathrm{J}\), is heat in \(R\).

Why the heat is the same for any \(R\)
\(R\) is not on the graph: the battery's line is \(V\) and the capacitor's line is \(\frac{q}{C}\), whatever \(R\) is. So the two triangles are always equal, and the heat is \(\frac{1}{2}CV^2\) for every \(R\).
A smaller or a larger resistance
The same answer comes from the current \(I = \frac{V}{R}\,e^{-t/RC}\); \(R\) cancels:
\[ H = \int_0^{\infty} I^2 R\, dt = \frac{V^2}{R}\int_0^{\infty} e^{-2t/RC}\, dt = \frac{V^2}{R}\cdot\frac{RC}{2} = \frac{1}{2}CV^2 . \]
The graph shows the heating power \(P = I^2R\) against time for the same battery and capacitor, with \(R =\) 1 k\(\Omega\) and with \(R =\) 4 k\(\Omega\). With 1 k\(\Omega\) the power starts at 100 mW and dies away within a few milliseconds. With 4 k\(\Omega\) it starts at only 25 mW and lasts four times as long.
The heat is the area under each curve, and both areas are 100 \(\mu\)J.

Careful
The heat is not \(CV^2\). \(CV^2\) is everything the battery gave; the capacitor keeps half of it, so the heat is the other half, \(\frac{1}{2}CV^2\).
It needs a full charge from zero. At half charge (\(q = \frac{Q}{2}\)) the battery has given \(\frac{1}{2}CV^2\), the capacitor holds only \(\frac{1}{8}CV^2\), and the heat so far is \(\frac{3}{8}CV^2\) (100, 25 and 75 \(\mu\)J in the example above).
Solved example
A 12 V battery fully charges an uncharged 5 \(\mu\)F capacitor through a 3 k\(\Omega\) resistor. Find the work done by the battery, the energy stored and the heat produced.
Battery. \(W = CV^2 = 5 \times 10^{-6} \times 144 = 720\ \mu\mathrm{J}\).
Capacitor. \(U = \frac{1}{2}CV^2 = 360\ \mu\mathrm{J}\).
Heat. \(H = W - U = 360\ \mu\mathrm{J}\). The 3 k\(\Omega\) is not needed.
720 \(\mu\)J, 360 \(\mu\)J, 360 \(\mu\)J
Key formulas
\[ \begin{aligned}\text{work by the battery (full charge from zero)} &= QV = CV^2\\[3pt] \text{energy stored} &= \frac{1}{2}CV^2 = \frac{Q^2}{2C} = \frac{1}{2}QV\\[3pt] \text{heat in the circuit} &= \frac{1}{2}CV^2 \quad \text{(any } R\text{)}\\[3pt] \text{two capacitors sharing charge: energy lost} &= \frac{1}{2}\,\frac{C_1C_2}{C_1 + C_2}\,(V_1 - V_2)^2\end{aligned} \]
Practice set
Easy to hard. The battery is ideal and every capacitor starts uncharged unless the question says otherwise. Try each one before you open its answer.
Quiz from the Short. A 5 V battery fully charges an uncharged 4 \(\mu\)F capacitor. The heat in the resistor is: (A) 100 \(\mu\)J (B) 50 \(\mu\)J (C) it depends on \(R\).
Show answer
(B) 50 \(\mu\)J. The battery gives \(CV^2 = 4 \times 25 = 100\ \mu\mathrm{J}\). The capacitor keeps \(\frac{1}{2}CV^2 = 50\ \mu\mathrm{J}\), so the heat is the other \(50\ \mu\mathrm{J}\), for any \(R\). 100 \(\mu\)J is the battery's whole work.
A 6 V battery fully charges a 10 \(\mu\)F capacitor. Find the work done by the battery and the energy stored in the capacitor.
Show answer
360 \(\mu\)J; 180 \(\mu\)J. \(W = CV^2 = 10 \times 36\); \(U = \frac{1}{2}CV^2\).
In question 2, how much heat is produced if the resistor is 2 k\(\Omega\)? And if it is 8 k\(\Omega\)?
Show answer
180 \(\mu\)J in both. The heat is \(\frac{1}{2}CV^2\) whatever \(R\) is; only the charging time changes.
A battery does 400 \(\mu\)J of work in fully charging a capacitor. How much energy does the capacitor store, and how much heat is produced?
Show answer
200 \(\mu\)J stored; 200 \(\mu\)J heat. \(W = CV^2\), so \(\frac{1}{2}CV^2\) is half of it, and the heat is the rest.
A 10 V battery charges a 2 \(\mu\)F capacitor. At the moment the capacitor holds half its final charge, find the work done by the battery so far, the energy stored and the heat so far.
Show answer
100 \(\mu\)J; 25 \(\mu\)J; 75 \(\mu\)J. \(q = 10\ \mu\mathrm{C}\): battery \(qV = 100\), stored \(\frac{q^2}{2C} = \frac{100}{4} = 25\), heat \(100 - 25 = 75\).
A 2 \(\mu\)F capacitor charged to 10 V is joined to an identical uncharged capacitor. Find the common voltage and the energy lost.
Show answer
5 V; 50 \(\mu\)J. The 20 \(\mu\)C spreads over 4 \(\mu\)F: \(V = 5\) V. Energy \(100\ \mu\mathrm{J}\) before, \(2 \times \frac{1}{2} \times 2 \times 25 = 50\ \mu\mathrm{J}\) after. Check: \(\frac{1}{2} \times \frac{2 \times 2}{2 + 2} \times 10^2 = 50\ \mu\mathrm{J}\).