[jsc-dev] Setting a memory limit and inspecting current memory usage

Saam Barati sbarati at apple.com
Thu Sep 13 15:07:34 PDT 2018



> On Sep 11, 2018, at 11:28 AM, Jérôme Gravel-Niquet <jeromegn at gmail.com> wrote:
> 
> Hey Saam
> 
>> - What's your goal here: to use less memory in general? Or to ramp up freeing memory when reaching a certain threshold?
> The latter. This is for a multi-tenant environment with a soft-limit
> on memory usage. After a while above the that limit we'd terminate the
> VM. So we definitely need to know the memory usage at times (could be
> a periodic callback, but ideally a function that can be called from
> any thread to get the currently used heap.)
> 
>> - What kind of platform are you running on? Do you JIT on this platform? 32 or 64 bit?
> This would run on linux, 64 bit. We'd be running the same VM + Context
> for the same usage over and over, so JIT should kick in (if that's
> what you were asking.)
> 
> If it helps, here's our specific use-case: We implement an interface
> similar to the ServiceWorker API to handle HTTP requests and respond
> to them asynchronously, all in JavaScript. We're currently running
> node.js and V8 for this: https://github.com/superfly/fly
> 
> We also run this in a multi-tenant environment and that's mainly why
> we need memory limits. We can enforce the limit ourselves (if we can
> inspect the current memory usage), but lowering the threshold for
> running the GC would be necessary.
Here are some places to start. WebKit lowers memory usage in a few ways:
- Based on what we think is our "memory limit".
- Based on receiving memory warnings.

We do these things in a few places. You should check out code pertaining to this. It should be easy to write your own custom logic tuned for your use case. Interesting places to look:
- bmalloc: scavenger behavior, grep for "isUnderMemoryPressure".
- Check out WTF::MemoryPressureHandler. You may want to implement your own mechanism for firing memory pressure events. You may also want to ensure you run the bmalloc scavenger on a memory pressure event (if we don't already do this).
- I believe we use WTF::ramSize to drive various control systems in JavaScriptCore/heap/Heap.cpp. You should look through that and see if you can tune it for your needs. You may want to tune WTF::ramSize itself.

If JavaScript runtime performance is not an issue for you (and memory is your primary concern), there are ways to make JSC use significantly less memory using a couple of knobs. What comes to mind would be:
- Turn off JITs.
- Turn on "mini" mode.

- Saam

> 
> 
> On September 11, 2018 at 1:45:36 PM, Saam Barati
> (sbarati at apple.com(mailto:sbarati at apple.com)) wrote:
> 
>> 
>> 
>>> On Sep 9, 2018, at 11:42 AM, Jérôme Gravel-Niquet wrote:
>>> 
>>>> We have no way of doing this currently.
>>> 
>>> How would you recommend doing it? If there's a way to inspect the
>>> memory usage of the VM, I might be able to implement logic for when to
>>> garbage collect and such. Sounds sub-optimal though.
>> This is indeed suboptimal. The GC already its own control system for this type of thing. You're better off tuning parameters in that control system than writing your own control system on top of it.
>> 
>> Let's start with what you're trying to achieve:
>> - What's your goal here: to use less memory in general? Or to ramp up freeing memory when reaching a certain threshold?
>> - What kind of platform are you running on? Do you JIT on this platform? 32 or 64 bit?
>> 
>> - Saam
>> 
>>> 
>>>> I don’t believe we provide API for this. A VM knows its approximate heap size. Are you building your own copy of JSC or linking against the system library?
>>> 
>>> Building my own from webkit's source
>>> 
>>> 
>>> 
>>> 
>>> On September 9, 2018 at 2:25:32 PM, Saam Barati
>>> (sbarati at apple.com(mailto:sbarati at apple.com)) wrote:
>>> 
>>>> 
>>>> 
>>>>> On Sep 7, 2018, at 11:47 AM, Jérôme Gravel-Niquet wrote:
>>>>> 
>>>>> Hey there,
>>>>> 
>>>>> I just started playing with JavaScriptCore and I think it's a great
>>>>> mix of flexibility and easy to use C API.
>>>>> 
>>>>> I couldn't find information on how to limit the memory usage
>>>>> (allocations) on a Context or ContextGroup. I'm mentioning these 2
>>>>> because there's no way to get at a VirtualMachine from the C API.
>>>> I believe the C API lets you access a context group, which is a VM.
>>>> 
>>>>> 
>>>>> Is there a way to set a memory usage limit with JSC?
>>>> We have no way of doing this currently.
>>>> 
>>>>> 
>>>>> Can I inspect a context or group of contexts for current memory usage?
>>>> I don’t believe we provide API for this. A VM knows its approximate heap size. Are you building your own copy of JSC or linking against the system library?
>>>> 
>>>> - Saam
>>>> 
>>>>> 
>>>>> If these are available through a non-C API, I might be able to create
>>>>> my own C wrapper for those.
>>>>> 
>>>>> Thanks,
>>>>> Jerome Gravel-Niquet
>>>>> _______________________________________________
>>>>> jsc-dev mailing list
>>>>> jsc-dev at lists.webkit.org
>>>>> https://lists.webkit.org/mailman/listinfo/jsc-dev
>> 



More information about the jsc-dev mailing list