The idle control valve adds airflow to the intake as needed to maintain correct idle. If it's clogged or malfunctioning, it may be causing these rpm fluctuations you're experiencing. It looks like a t-valve, located above the engine block just under the intake manifold. Another possibility is a defective throttle position sensor, which affects the operation of the ICV. I'm not sure if you have one tho; the newer E39's use drive-by-wire throttle systems which I believe eliminated the TPS.