Whenever any legacy shall be charged upon or payable out of real estate, the heir or devisee before paying the same shall deduct the tax therefrom and pay the same to the executor, administrator or trustee. The tax shall remain a charge upon the real estate until paid, and the payment thereof shall be enforced by the executor, administrator or trustee in the same manner that the payment of the legacies might be enforced. If, however, the legacy be given in money to any person for a limited period, he shall retain the tax upon the whole amount, but, if it be not in money, he shall make application to the court having jurisdiction of his account to make apportionment, if the case requires it, of the sum to be paid into his hands by such legatees, and for such further order relative thereto as the case may require.