# How to override the .new procedure?

**URL:** <https://forum.crystal-lang.org/t/how-to-override-the-new-procedure/8036>\
**Category:** Help & Support\
**Created:** [May 3, 2025, 7:44pm UTC](https://forum.crystal-lang.org/t/how-to-override-the-new-procedure/8036 "2025-05-03T19:44:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alectronic](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Alectronic](https://forum.crystal-lang.org/u/Alectronic)\
**Post date:** [May 3, 2025, 7:44pm UTC](https://forum.crystal-lang.org/t/how-to-override-the-new-procedure/8036/1 "2025-05-03T19:44:29Z")

</div>

Next step in creating my own prelude. Let’s say I have a custom allocator and want to use that to create class instances by calling` .new`. But even with an empty prelude Crystal seems to use an internal allocator, defining `def new` in `class Object` doesn’t help.

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [May 3, 2025, 10:03pm UTC](https://forum.crystal-lang.org/t/how-to-override-the-new-procedure/8036/3 "2025-05-03T22:03:55Z")

</div>

I realized I misunderstood your question, so I deleted my earlier reply.  
I can’t really answer something this complex myself, but I found that [DeepWiki](https://deepwiki.com/crystal-lang/crystal) can be helpful in cases like this. It looks through the codebase and gives relevant information.

[For example, instead of overriding `.new`, it points out that you can use `pre_initialize` to implement a custom allocator.](https://deepwiki.com/search/relevantcontextthis-query-was_c633acdb-b16c-4bb5-bddb-59ac00d66426)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [May 4, 2025, 5:30pm UTC](https://forum.crystal-lang.org/t/how-to-override-the-new-procedure/8036/4 "2025-05-04T17:30:09Z")

</div>

`pre_initialize` is for when you want to explicitly manage allocation of a particular instance.

I presume you want to customize _all_ allocations for _all_ instances of _all_ classes, though. For that, you need to define `GC.malloc`, `GC.malloc_atomic`.  
For reference, see the [implementation in `gc/none`](https://github.com/crystal-lang/crystal/blob/48f3c1ea3ba2505909286372ef756ae387cadca0/src/gc/none.cr#L12-L51). These methods are called implicitly as part of the autogenerated `.new` methods (through `.allocate` and ` __crystal_malloc64`, `__ crystal_malloc_atomic64`).

If you would want to customize _all_ allocations behaviour for _all_ instances of a specific class, you could override the respective `.allocate` method instead.
