Raymond Wallace is likely one of the most expensive county jail inmates in Massachusetts ever.

The 41-year-old detainee has been awaiting trial and held on and off for more than four years in a locked wing of the Lemuel Shattuck Hospital in Jamaica Plain, which annually costs about $284,000 a year per inmate.

The Essex County Sheriff’s Department says it has spent about $2 million in overtime costs to guard him since 2013 when he was shot repeatedly in the abdomen by a deputy sheriff while attempting to escape from a Boston hospital where he was receiving treatment for an injury.

Since then, Wallace has had several surgeries and is outfitted with drains to release waste, according to court records.

“He’s lost about 100 pounds. He needs to be fed via a tube,” said Wallace’s attorney Raymond Buso of Salem last week. “He’s extremely weak.”

Wallace’s case shows how expensive it can be to care for sick inmates, especially in the county jail system that isn’t set up for long-term care. County jails incarcerate nearly 11,000 inmates who are awaiting trial or serving sentences of up to 2 ½ years. In contrast, the state prison system incarcerates some 9,200 inmates, including those serving life sentences, most of whom are there for longer periods of time.

In April, Gov. Charlie Baker signed legislation that would allow certain inmates to be released if they can prove they are too sick to pose a safety risk. County sheriffs say they also struggle to care for aging and sick inmates and many publicly supported the new law. But it’s unclear whether there is a place for the release of someone like Wallace who prosecutors say is dangerous even in what appears to be his debilitated state.

“Mr. Wallace has demonstrated himself to be a dangerous person when he is outside of supervised custody,” said Carrie Kimball Monahan, a spokesperson for the Essex County District Attorney’s office. “Our duty is to protect the public as well as to ensure justice for Mr. Wallace.”

Wallace is in legal limbo as his attorneys argue he is too sick to be tried. He has cases pending in Essex and Suffolk counties. A Suffolk County Superior Court judge agreed in 2016 that scheduling a trial for Wallace was “premature,” and the case is ongoing. A status hearing is scheduled for June.

Wallace is awaiting trial for his attempted escape and allegedly shooting a deputy sheriff at the Massachusetts Eye and Ear infirmary. At the time, he was awaiting trial after being charged with two armed robberies in 2010 and 2011.

The high cost of Wallace’s incarceration came up last summer during a hearing on the medical parole legislation. Essex County Sheriff Kevin Coppinger testified that he had paid $1.35 million in overtime to guard the inmate at different hospitals.

“Extraordinary costs like this seriously hamper the efficiency and effectiveness of our operation,” Coppinger said in written testimony. He said such expenses can be “crushing.”

Costs have continued to mount. In January, the state Department of Correction that runs the Shattuck hospital agreed to lower Wallace’s threat level to allow state corrections officers already on site to guard him after a story by the Salem News detailed the high cost of security to Essex County.

Department of Correction officials said Monday that Wallace was classified as a “Level A” escape risk because of his prior escape attempt. There are currently no county officers guarding inmates at the Shattuck hospital.

While Wallace’s case appears extreme, many sheriffs say counties are challenged by the increasing financial burdens of incarcerating aging and ill inmates. Costs of transporting them to the hospital can add up, as two officers are needed for every trip, said Middlesex County Sheriff Peter Koutoujian, who heads a state association of county sheriffs.

Koutoujian has long been a supporter of compassionate release for terminally ill inmates. Twenty-two convicted inmates died from chronic or long-term medical issues in the state’s 13 county jails between 2010 and mid 2015, according to the jail association. This does not include those who had not yet been convicted, county officials say.

“These are people with greater medical needs and have greater costs and greater issues regarding security,” Koutoujian said, adding that, in many cases, they should be placed elsewhere. “If you are terminally ill or medically incapacitated, you shouldn’t have to be in jail, number one, and you don’t need to be in jail, number two,” he said.

Barnstable County Sheriff James Cummings told lawmakers last summer that transporting one terminally ill inmate to medical appointments can cost the department more than $300,000 over a nine-month period. Norfolk County Sheriff Michael Bellotti testified that in many cases “death with dignity outside the walls of jail would be preferential.’’

The new medical parole law allows prison superintendents and county sheriffs to petition the Department of Correction commissioner if they believe an inmate is so sick they no longer pose a safety risk and have a place to go. Corrections officials did not respond to questions whether the program would apply to inmates like Wallace who haven’t yet been convicted.

Either way, it appears unlikely Wallace will be going anywhere. Even his attorney, Buso, acknowledges this. “He has serious open cases in Essex county and Suffolk county,’’ Buso said. “There would be so many people who would be against it.”

The New England Center for Investigative Reporting is a nonprofit news center based out of Boston University and WGBH News. Ying Wang and Ziwei Guo contributed to this report.