We have a requirement where we have to merge slides from multiples presentation into a single presentation. But when we load the presentation using with statement and exit the with statement the memory loaded by the presentation is not release by the RAM causing a Out of memory error.
What the context tells us
- The user merges many presentations with Aspose Slides for Python via .NET.
- After exiting a
withblock the RAM used by each loaded presentation is not freed, eventually causing an out‑of‑memory error.
What is not in the provided context
- No answer or official guidance from Aspose is included, so the exact disposal behavior of the Python .NET wrapper is not documented in the excerpt.
Likely cause & practical steps (assumption)
| Step | Why it helps | How to apply (Python .NET) |
|---|---|---|
Explicitly dispose the Presentation object |
The .NET Presentation class implements IDisposable. In pure C# a using block calls Dispose, but the Python with statement does not automatically invoke Dispose on .NET objects. |
python\nimport aspose.slides as slides\n\nfor file_path in source_files:\n pres = slides.Presentation(file_path) # load\n # … merge slides …\n pres.dispose() # release native resources\n del pres # remove reference\n import gc; gc.collect() # force Python GC\n |
| Process presentations one‑by‑one | Keeping many Presentation instances alive simultaneously multiplies memory usage. |
Load a source, copy needed slides to the target, then dispose before moving to the next source (as shown above). |
| Reuse a single target presentation | Creating a new target for each merge adds overhead. | Create the target once (target = slides.Presentation()), then repeatedly target.slides.insert_clone(...) from each source, disposing the source after each insertion. |
| Upgrade to the latest Aspose Slides version | Memory‑leak fixes are released periodically. | Verify you are on the newest version (e.g., 24.x) and, if not, update via pip install aspose.slides --upgrade. |
| Enable .NET garbage collection hints (optional) | For long‑running processes you can request a full collection after a batch. | python\nimport gc\ngc.collect()\n |
What to check next (if the above does not solve the issue)
- Version information – which Aspose Slides for Python via .NET version are you using?
- Exact code snippet – a minimal reproducible example (loading, merging, exiting the
withblock). - Environment details – OS, .NET runtime (Core/Framework), Python version, and whether you run 32‑bit or 64‑bit.
Providing these details will let us confirm whether the memory is held by the native Aspose library or by Python references, and suggest any additional configuration (e.g., Presentation.dispose() alternatives or LoadOptions tuning).
If you have already tried dispose() or forced garbage collection without success, please share the code you used and the Aspose Slides version so we can investigate further.
Line # Mem usage Increment Occurrences Line Contents
=============================================================
47 299.4 MiB 299.4 MiB 1 @mp.profile
48 def clone_slide(self, source: str, source_slide_no:int, comments:str, dest_slide_no: int):
49 299.4 MiB 0.0 MiB 1 load_options = LoadOptions()
50 # Choose the KeepLocked behavioróthe presentation file will remain locked for the lifetime of
51 # the Presentation instance, but it does not need to be loaded into memory or copied to a temporary file.
52 299.4 MiB 0.0 MiB 1 load_options.blob_management_options = BlobManagementOptions()
53 299.4 MiB 0.0 MiB 1 load_options.blob_management_options.presentation_locking_behavior = PresentationLockingBehavior.KEEP_LOCKED
54 299.4 MiB 0.0 MiB 1 load_options.blob_management_options.is_temporary_files_allowed = True
55 299.4 MiB 0.0 MiB 1 load_options.blob_management_options.max_blobs_bytes_in_memory = 40 * 1024 * 1024 # 10 MB
56 299.4 MiB 0.0 MiB 1 if self.once:
57 663.4 MiB 337.4 MiB 2 with Presentation(source, load_options) as source_ppt:
58 665.9 MiB 26.6 MiB 1 self.dest_ppt.slides.add_clone(source_ppt.slides[source_slide_no-1])
59 665.9 MiB 0.0 MiB 1 self.author.comments.add_comment(comments, self.dest_ppt.slides[dest_slide_no], draw.PointF(0.2, 0.2), datetime.date.today())
60 665.9 MiB 0.0 MiB 1 del source_ppt
61 663.4 MiB 0.0 MiB 1 gc.collect()
62 #self.dest_ppt.save(self.dest_tfname, export.SaveFormat.PPTX)
63 else:
64 self.dest_ppt = Presentation()
65 self.author = self.dest_ppt.comment_authors.add_author(“Playbook”, “MF”)
66 with Presentation(source, load_options) as source_ppt:
67 self.dest_ppt.slides.add_clone(source_ppt.slides[source_slide_no-1])
68 self.author.comments.add_comment(comments, self.dest_ppt.slides[dest_slide_no], draw.PointF(0.2, 0.2), datetime.date.today())
69 del source_ppt
70 gc.collect()
71 #self.dest_ppt.save(self.dest_tfname, export.SaveFormat.PPTX)
72 self.once = True
73 663.4 MiB 0.0 MiB 1 gc.collect
We are also used dispose but that is not recognised by the Aspose.slides.Presentation. We are using following environment:
aspose.slides == 25.12.0
Base_image_container = python:3.10-bullseye
@akhil.rana,
Thank you for contacting free support. We need more details to investigate the case. Please share the following:
- The sample presentation files.
- The code example to reproduce the problem.