Buffalo wanted him out due to his cap numbers ($8.5M in 2014, $8.85 in '15, and $8.95M in '16); now they just have to eat the rest of his prorated bonus ($8.475M). The Bills get virtually no cap savings with this trade this season, but they save plenty over the next two years.
The 49ers get a bargain because they're only on the hook for a $3.65M salary this year; it's a one-year rental, with an audition to see if he's worth $5.5M next year and $5.85M in 2016. That's pretty good value in exchange for a 4th round pick, in my opinion.