No person shall be imprisoned within the prison walls of any jail for a longer period than three months under an execution or any other mandate against the person to enforce the recovery of a sum of money less than five hundred dollars in amount or under a commitment upon a fine for contempt of court in the nonpayment of alimony, maintenance, distributive awards or special relief in matrimonial actions or counsel fees in a divorce case where the amount so to be paid is less than the sum of five hundred dollars; and where the amount in either of said cases is five hundred dollars or over, such imprisonment shall not continue for a longer period than six months. It shall be the duty of the sheriff in whose custody any such person is held to discharge such person at the expiration of said respective periods without any formal application being made therefor. No person shall be imprisoned within the jail liberties of any jail for a longer period than six months upon any execution or other mandate against the person to enforce the recovery of a sum of five hundred dollars or over or for a longer period than three months where the amount is less than five hundred dollars, and no action shall be commenced against the sheriff upon a bond given for the jail liberties by such person. In computing the term of imprisonment time spent within the prison walls of any jail and time spent within the jail liberties shall be included. Notwithstanding such a discharge in either of the above cases, the judgment creditor in the execution, or the person at whose instance the said mandate was issued, has the same remedy against the property of the person imprisoned which he or she had before such execution or mandate was issued; but the prisoner shall not be again imprisoned upon a like process issued in the same action or arrested in any action upon any judgment under which the same may have been granted. Except in a case hereinbefore specified nothing in this section shall affect a commitment for contempt of court.