James Gandolfini, the actor who brought the Jersey mob boss to life, admits that he was as confused as everyone else by the finale of “The Sopranos,” according to a new, wide-ranging oral history of the HBO show in April’s Vanity Fair, which will hit newsstands today.

“When I first saw the ending, I said, ‘What the f–k?’” Gandolfini said. “I mean, after all I went through, all this death, and then it’s over like that?”

Though the ending of “The Sopranos’ ” last episode — in which the screen suddenly goes black as the mob boss sits with his family in a diner — has been much maligned, Gandolfini eventually came to like it.

“After I had a day to sleep, I just sat there and said, ‘That’s perfect.’ ”

The Vanity Fair oral history of the show features dozens of recollections from former cast members, Sopranos writers and show creator David Chase.

Lorraine Bracco, who played Dr. Jennifer Melfi on the show, also didn’t appear to be thrilled by the ending.

“I would have wanted it to end differently,” she told Vanity Fair. “But God knows we’ve talked about that ending for five years now — we’re still talking about it. People stop me in the street. ‘Did you get the ending? Did I miss something?’ I thought it was very, very shrewd.”

The magazine piece is packed with stories of the Mafia show’s creation, such as Drea de Matteo’s hatred of her faux New Jersey accent.

“I felt like my accent sounded really, really fake,” said the actress, who played mob moll Adriana La Cerva. “Now when I walk down the street, people say, ‘Just give me one Chris-ta-fuh.’ ”

De Matteo’s Jersey transformation was particularly amazing, since she wasn’t initially considered for the Adriana role because Chase told her, “You don’t look Italian. You look like a hostess of a restaurant.”

When de Matteo got a second chance to try out for the role, she learned her lesson and came dressed up like a proper New Jersey Guidette.

“At this point I knew what I was dealing with,” she said. “So I wore my nameplate in diamonds. I teased my hair up a little bit. One of the words in the line was “Ow,” and the reason that I got the part was because the way I said ‘Owwuhwhwwwuhwwwuh!’ I turned it into, like, five syllables.”

“As a mob boss, the guy was totally unreliable! . . . Tony put up with him for too long,” he said about Moltisanti, who was killed by Tony in the final season. “Christopher just spelled the end of Tony, his family — everything. From my standpoint, as the architect of the series, Tony put up with him for too long.”

Still, Chase said that killing off characters was something he dreaded and that the process of pulling the trigger on one of his actors made him feel like a real Godfather.

“It was a hard thing to do,” he said. “But at the same time, I thought to myself, well, I’m writing about a guy who’s the boss of a Mafia family, and he has to do these things, too.”

The violence on the show even had some of the actors feeling like they had to live up to a real-life code of the streets.

Tony Sirico pleaded with Chase to not make his Paulie “Walnuts” character kill a woman.

“David, I come from a tough neighborhood,” he said. “If I go home and they see that I killed a woman, it’s going to make me look bad.”

Chase refused to change the script.

“Here’s the thing. We did the scene,” Sirico recalled. “I had to smother her. First he wanted me to strangle her; I said, ‘No, I’m not putting my hands on her.’ He said, ‘Use the pillow.’ After it was all said and done, I went back to the neighborhood and nobody said a word.”

Because the show was so much like the real mob, none of the actors knew when they were about to get whacked — and it led to nervous moments whenever scripts were handed around.

“If it’s time for your character to go, it’s time for your character to go. It doesn’t matter who you are,” said actor Steve Schirripa, who played Tony’s tubby bodyguard, Bobby “Bacala.”

“I mean, this wasn’t ‘Friends.’ This was a real worry,” he said. “You know, we would talk. ‘Did you hear anything?’ You’re asking the writers. Nobody’s telling you nothing. Each time the script arrived, you go to the front, you go to the back, looking.”

One of the saddest moments on the set was the day Vincent Pastore’s character Sal “Big Pussy” Bonpensiero got bumped off.

“We lost Vinny Pastore, and that was the most difficult day up to that point. That was the only day on the set I can remember, you know, out of the whole 10 years where it was very uptight, a lot of tension on the set,” said Steve Van Zandt, who played mob consigliere Silvio Dante.

“I remember people yelling at each other and really pissed off, which you never saw,” he said. “Why? Because we were losing Vinny. He was a beloved friend of ours, and you’re not going to see him anymore.”

A happier day on the set was when — after winning a big payday in contract negotiations — Gandolfini handed out gifts of cash to the other regular actors.

“After Season 4, Jim called all the regulars into his trailer and gave us $33,333 each, every single one of us,” Schirripa said. “ Now, there were a lot of big actors—Kelsey Grammer, Ray Romano—and they’re all nice guys, I’m sure, but nobody gave their cast members that kind of money. That’s like buying everybody an SUV. He said, ‘Thanks for sticking by me.’ ”

Edie Falco recalled how playing Tony’s wife made her feel like she was becoming the mob kingpin’s real spouse — to the extent that she started becoming possessive of Gandolfini.

“It was weird to sit down at a table and read with the actresses playing Tony’s girlfriends. Occasionally I would get a sharp twinge at the back of my neck,” she recalled.

“Even years later, I remember when I saw Jim in “God of Carnage” on Broadway, and he was Marcia Gay Harden’s husband, and I had this ‘How come I have to be OK with this?’ kind of feeling.”

As for the show’s mysterious ending, Chase defended it by comparing it to movies such as “8 1/2” and “Raging Bull,” saying “ambiguity was very important to me.”

“The Sopranos was ambiguous to the point where, to this day, I’m not really sure whether it was a drama or a comedy,” he said.

“It can be both, but people like to reduce it to one or the other. I know there are the two masks, Comedy and Drama, hanging together. But that’s not the way American audiences seem to break things down.”

While most of the response to “The Sopranos” has been positive from fans and reviewers alike, Gandolfini took umbrage at critics who thought the show celebrated Mafia violence.

“We’d get accused, back then, of glamorizing mobsters, but we were all half-miserable, you know. I don’t think the violence looks appealing at all.

“Everyone paid for the violence in a lot of ways . . . It’s a very violent world and, you know, there’s consequences. I think we showed it, and I think we showed the toll it takes on these people.”