North Korea's leader Kim Jong Un makes a statement in this photo released on Sept. 22. (Kcna Kcna/Reuters)

Blaine Harden, a former Post reporter, is the author of "King of Spies: The Dark Reign of America's Spymaster in Korea" and a consultant to the Frontline documentary "North Korea's Deadly Dictator" airing on PBS on Oct. 4.

To the frustration of President Trump, China seems to be losing leverage over the young dictator next door. North Korean leader Kim Jong Un keeps biting the hands of Beijing elders who provide his otherwise friendless state with fuel, food and diplomatic cover.

Those bites have challenged China's authority, embarrassed its leaders and stymied U.S.-backed efforts by Beijing to restrain Kim's flirtation with nuclear war. Several recent incidents suggest a growing trend:

• North Korean spies are prime suspects in the sensational nerve-agent killing early this year in Malaysia of Kim's half brother, Kim Jong Nam, a resident of the Chinese territory of Macau, where he was under the unofficial protection of China. As Frontline reports this week, Kim Jong Nam had supported Chinese-style economic reforms in North Korea and was seen by some as a possible replacement for Kim Jong Un.

• The detonation in September of North Korea's largest nuclear device, which it said was a hydrogen bomb, disrupted a summit of world leaders in Beijing, embarrassing Chinese President Xi Jinping. It wasn't the first time. North Korea launched a missile in May just before Xi was to speak to dignitaries. And Xi's new envoy for North Korean relations has not even been allowed into the country.

• Ignoring repeated warnings from China, Kim has accelerated the frequency and increased the range of long-range ballistic missile tests. At the same time, his regime has blamed Beijing for a "reckless act of chopping down" trust between the countries.

What motivates Kim to alienate the affections of an ally that accounts for 90 percent of North Korea's trade? What does he have against China?

Totalitarian leaders usually don't explain themselves, and Kim — six years in power and only 33 — is no exception. But insights into his Sino-belligerence can be gleaned from the back story of his family. In nearly 70 years of dealing with their powerful patrons in communist China, three generations of dictators named Kim have lurched between dependence and distrust, cooperation and contempt. The family's unruly behavior suggests there is little reason to assume — as Trump and other U.S. presidents sometimes have — that China can impose its will on North Korea.

The edgy attitude of the Kim family is likely rooted in the arrest and humiliation at Chinese hands of Great Leader Kim Il Sung, North Korea's founding despot and the late grandfather of Kim Jong Un.

Kim Il Sung grew up in northeast China, where in the 1930s he became a guerrilla leader and fought alongside Chinese communist partisans against Japanese occupiers. Without warning, local communists turned on Kim and his men. Several hundred ethnic Koreans were tortured and murdered in a racist purge based on the party's paranoid, and false, belief that they were secretly working with the Japanese.

Kim was arrested in China in 1934 and was lucky to survive. He later called the purge "a mad wind. . . . [Koreans] were being slaughtered indiscriminately by [Chinese] with whom they had shared bread and board only yesterday."

During the Korean War, Kim's bitter memories were compounded by a painfully public loss of face. Kim started the war in 1950 by invading South Korea with the backing of Stalin's Soviet Union. But his army was soon obliterated by a U.S.-led coalition, and North Korea all but disappeared — until Chinese forces entered the fight and forced Kim to the sidelines of his own war. China's top general, Peng Dehuai, chided Kim for his "extremely childish" leadership, telling him, "You are hoping to end this war based on luck."

Kim would never forget how he was treated. After the war, he made sure that China's role in saving and rebuilding his state was largely erased from official histories. His resentment was compounded in 1980, when China publicly denounced as feudalism his decision to transfer absolute power to his son Kim Jong Il, a succession that made North Korea the world's only hereditary communist kingdom.

Ill feelings between North Korea and China have often been mutual. Mao Zedong regarded Kim Il Sung as rash and doctrinaire — once describing him as "a number-one pain in the butt," Sidney Rittenberg, onetime translator for Mao, told me in 2013. In 1992, China infuriated the Kim family by establishing diplomatic relations with South Korea, the archenemy of the North.

Still, Chinese leaders from Mao to Xi have chosen to protect and prop up the troublesome Kim regime, fearing that its collapse would trigger a flood of refugees and lead to a U.S.-allied, united Korea on its border. As North Korea's international isolation has increased along with its nuclear prowess, the Kim regime has become more dependent than ever on Chinese trade.

That dependence, however, is clearly not swaying the behavior of Kim Jong Un. He seems to have internalized his grandfather's grudges. Last month, when Kim was calling Trump a "mentally deranged U.S. dotard," a commentary in the Korean Central News Agency attacked the media in Beijing for being "rude" and "shameless." It also said that North Korea owed little to China.

The commentary was signed by "Jong Phil," which means "righteous pen," and was the third such anti-China article written by him this year. A China scholar, Adam Cathcart at the University of Leeds, told The Post that he believes Jong Phil is a pen name for Kim.