Image copyright Rex Features

Author JG Ballard was interned in World War II China as a teenager - an experience which formed the basis of his best-selling novel Empire of the Sun. How did the memories of that early trauma influence his fiction, asks philosopher John Gray.

Looking for his best friend as a teenager in an apartment block in Shanghai, the writer JG Ballard remembered "suddenly finding that the building was totally empty, and wandering around those empty flats with the furniture still in place, total silence, just the odd window swinging in the wind".

Around the same time, while he was roaming around the city's waterfront, Ballard rowed out to the ships, freighters and steamers that had been sunk to form a boom in the harbour.

As he recalled in an interview, "I remember walking onto the decks, with water swilling through the staterooms. Given the stability of the society we now live in, this is very difficult to convey."

Even more than the time he spent in the Japanese prison camp outside Shanghai, Ballard's experience of a great city from which all signs of normal life had disappeared shaped him for the rest of his life.

When asked why it took him so long to write Empire of the Sun, a fictionalised version of his early years that Steven Speilberg turned into a highly successful film, Ballard said, "It took me 20 years to forget, and 20 years to remember."

Forgetting Shanghai was part of life for him in post-war Britain, where he arrived with his mother in 1946.

At least for a Westerner, Shanghai was an intensely exciting place, with dozens of radio stations, unlimited advertising and extremes of wealth and poverty reflected in an ever-changing media landscape.

In some ways the city was an extreme version of the world we now inhabit. The contrast with Britain in the immediate aftermath of WWII was sharp - and in Ballard's eyes, the differences were not always to Britain's advantage.

Arriving in Portsmouth, he noticed what appeared to be coal scuttles moving about on the docks, only to realise that the small moving objects were actually motor cars.

The pokey British cars, so different from the spacious Cadillacs he used to see cruising around in Shanghai, seemed to him - then and later - to epitomise the cramped and shabby country he had entered.

Image copyright Getty Images Image caption After World War II Britain saw widespread shortages and rationing

There was a more fundamental reason why Ballard needed to forget Shanghai.

His experiences there had been traumatic, and if they revealed to him the flimsiness of everyday existence, it was a revelation that he found hard to live with. He often used to say that human societies are like stage sets, which seem substantial enough until the props are abruptly overturned and the stage is left empty and abandoned.

In any longer view, the endemic instability of human institutions is clear, but it is denied by all of those who project an illusion of progress back into history and forwards into the future.

Ballard was never tempted to succumb to the comforting fantasy that order in society is slowly increasing, but like any other human being, he needed to find meaning in the incidents of his life. He achieved this by transforming the desolate scenes he had witnessed as a child into the fantastic and lovely images that fill his books.

JG Ballard in brief 1930: Born in Shanghai to British parents

Born in Shanghai to British parents 1943-45: Interned in Japanese camp

Interned in Japanese camp 1946: Returned to Britain with mother

Returned to Britain with mother 1949: Studied medicine at Cambridge but left university early

Studied medicine at Cambridge but left university early 1953: Joined RAF, based in Canada flight training centre for six months

Joined RAF, based in Canada flight training centre for six months 1962: First novel published

First novel published 1984: Empire of the Sun published

Empire of the Sun published 2009: Died in London, England BBC News obituary on JG Ballard

As critics have noted, the same scenery recurs throughout Ballard's writings.

Drained swimming pools, low-flying aircraft and eerily deserted hotels and casinos appear in many of his novels and short stories.

Some have found Ballard's landscapes repetitive and obsessive, but for me they have enormous power because they invest scenes of desolation with life-affirming meaning and beauty. He hadn't truly forgotten the empty apartments in which as a boy he had searched for his friend.

Images of what he had seen remained in his mind, many of them too painful to be allowed into full awareness. But even if they couldn't be directly accessed by him, these memories continued to be active and returned to him again and again through the channel of his fiction.

In The Garden of Time, a short story published in 1962, Ballard portrays two aristocrats, Count Axel and his wife, besieged in an exquisite castle garden by an advancing mob.

The garden contains crystalline flowers at the tops of whose stems are goblet-shaped blossoms, which when snapped from the stem, drain the garden of time and motion. Each evening the count snaps off one of the time flowers.

As he carries the crystal blossom onto the terrace, the menacing rabble retreats and he and his wife are safe for another day. But the stems no longer bear fruit, the flowers are being used up, and the count realises that time could not be stopped forever.

When the last flower has been picked and the horde finally reach the castle, the garden contains two stone statues, a man and a woman side by side, looking calmly over the castle grounds, the woman clasping a single, almost transparent flower.

While this enchanting fable can be interpreted in a number of ways, I'm inclined to read it as an allegory of the contradictions of memory.

If our memories lend a pattern to our lives, they also condemn us to repeat the past - especially when they concern traumatic events. We struggle to rid ourselves of the traces that are left in us by painful episodes in our lives; but these buried images never go away, only shift their shapes.

Image copyright Getty Images Image caption JG Ballard was deeply affected by what he saw in Shanghai in the 1940s

Whether conscious or not, they're constantly mutating and however much we want to banish them, they keep on coming back to us.

Like the seething mob advancing on the aristocrats in their castle, our memories threaten the peace of mind we so dearly cherish. We look for a talisman against memory, some magic that can reverse the course of time.

But the magic doesn't really work, and if we persist in the struggle to keep our memories at bay we may come to resemble the stone statues in the garden, transfixed and petrified by the past.

In Ballard's case the magic was the power of his imagination, and unlike the crystal blossoms of his story it brought him life.

Through a kind of inner alchemy, the Shanghai of his childhood became the London of his first major novel The Drowned World, also published in 1962.

Irreversibly altered by climate change so that it has become a region of tropical lagoons and advancing jungle, the city is almost unrecognisable, though the weed-choked streets remain intact in the depths of the lagoons and the upper floors of a few crumbling hotels continue to be habitable.

Like many of Ballard's characters, the novel's central protagonist - a biologist who shares many of Ballard's own preoccupations with time and memory - doesn't regret the passing of the old world. At the end of the novel he finds fulfilment in the sun-filled wilderness that is swallowing up the past.

Memories of Shanghai as he knew it as a child shaped Ballard's writings for the following 20 years, filling them with scenes that seem to exist out of time.

In 1984 he published Empire of the Sun and in 1991 The Kindness of Women, novels in which his childhood and later life were re-imagined in terms closer to the actualities of his experience.

In 2008, a year before he died, he retold the story more directly in his memoir Miracles of Life.

Empire of the Sun on screen Released in 1988, directed by Steven Spielberg

Said to be first major Hollywood production to be filmed in People's Republic of China

JG Ballard's novel adapted to screen by playwright Tom Stoppard

Stars Christian Bale as young lead Jim Graham

Cast includes John Malkovich, Nigel Havers and Miranda Richardson

The life he had struggled to forget he was now free to tell from memory.

The workings of memory seem contradictory because they obey conflicting human impulses.

By preserving our experiences in recollection, we conserve the meaning in our lives. If we couldn't look back, the present would be empty, a recurring moment without significance.

We have a picture of ourselves as acting meaningfully only because we can reconstruct our lives as a succession of events linked in recollection.

But what we remember isn't a matter of will or choice, and what is retained in the mind can easily become a burden. While we struggle to preserve the past, we can at the same time long to leave it behind.

Inwardly Ballard never did leave Shanghai, but in time the fact ceased to trouble him.

Through the alchemy of memory, the leaden buildings in which he'd wandered as a boy became the golden vistas of his fiction, and the traumas of his childhood were transmuted into images of fulfilment.