What I’m trying to do:
I’m using uplink server to do some calculations using QuantLib (a package in C++ with SWIG). The calculation in the uplink server takes a lot of memory. I notice that when the calculation is completed and result is returned to Anvil, the memory is not released to OS.
I use htop to monitor the memory usage of the uplink server process.
What I’ve tried and what’s not working:
Can’t think of anything I can do.
Uplink Server Code Sample:
import anvil.server
import QuantLib as ql
anvil.server.connect("server_YTY7C3R3AIR2TCB35DL3DJBV-XCYMXVY7YLI4MHKO")
@anvil.server.callable
def run_test(num):
result = consume_memory(num)
print('Run completed!')
return result
def consume_memory(num):
# Create a list to hold many MemoryConsumer objects
objects = []
# Create 1 million objects, each consuming a significant amount of memory
for i in range(num):
objects.append(create_asset(1000))
return ("Created a lot of objects to consume memory.")
def create_asset(notional):
# Step 1: Set the valuation date
valuation_date = ql.Date(31, 8, 2024)
ql.Settings.instance().evaluationDate = valuation_date
# Step 2: Define the calendar, conventions, and day count
calendar = ql.TARGET()
business_convention = ql.ModifiedFollowing
day_count = ql.Actual360()
# Step 3: Define the overnight index (e.g., EONIA for EUR, FedFunds for USD)
eonia_fixing_date = ql.Date(30, 8, 2024)
eonia_rate = 0.0015 # Example rate
# Add the fixing to the Eonia index
overnight_index = ql.Eonia()
overnight_index.addFixing(eonia_fixing_date, eonia_rate)
fixed_rate = 0.01 # Example fixed rate of 1%
flat_forward_rate = ql.FlatForward(valuation_date, ql.QuoteHandle(ql.SimpleQuote(fixed_rate)), day_count)
discount_curve_handle = ql.YieldTermStructureHandle(flat_forward_rate)
overnight_index = ql.Eonia(discount_curve_handle)
# Step 4: Define the fixed leg parameters
fixed_rate = 0.01 # Example fixed rate of 1%
fixed_frequency = ql.Annual
fixed_day_count = ql.Thirty360(ql.Thirty360.BondBasis)
# Step 5: Define the swap maturity
start_date = valuation_date
maturity_date = calendar.advance(start_date, ql.Period(2, ql.Years)) # 2-year OIS
# Step 6: Define the notional amount
# notional = 1000000 # Example notional of 1,000,000
# Step 7: Create the fixed leg schedule
fixed_schedule = ql.Schedule(
start_date,
maturity_date,
ql.Period(fixed_frequency),
calendar,
business_convention,
business_convention,
ql.DateGeneration.Forward,
False
)
# Step 8: Create the Overnight Indexed Swap (OIS)
ois = ql.OvernightIndexedSwap(
ql.OvernightIndexedSwap.Payer, # Swap type (Payer/Receiver)
notional,
fixed_schedule,
fixed_rate,
fixed_day_count,
overnight_index,
)
# Step 9: Set up a discount curve for pricing (using the fixed rate as the discount rate for simplicity)
discount_curve = ql.FlatForward(valuation_date, ql.QuoteHandle(ql.SimpleQuote(fixed_rate)), day_count)
discount_curve_handle = ql.YieldTermStructureHandle(discount_curve)
# Step 10: Set the pricing engine and calculate NPV and fair rate
ois_engine = ql.DiscountingSwapEngine(discount_curve_handle)
ois.setPricingEngine(ois_engine)
# Output the results
# print("NPV of the OIS:", ois.NPV())
# print("Fair fixed rate:", ois.fairRate())
return ois
anvil.server.wait_forever()
Clone link:
Anvil | Login